Tag Archives: coding practice

check if a tree is a bst

int inorderCheck( TreeNode *root, int *last ) { if( root == NULL ) { *last  = INT_MIN; return 1; } int left = inorderCheck( root->left, last ); if( root->data > *last && *last != null ) { *last = root->data; … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

given in-order and pre-order, re-construct a tree

Here is my code :   //    In-order traversal : c b f d g a e  (Left, Root, Right)//     Pre-order traversal : a b c d f g e. (Root, left, right)////    static int rootIdx = 0;    public Node … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

sort a linkedlist in place

hint : bubble sort would be the most simple in place one

Posted in Uncategorized | Tagged , | Leave a comment

amazon set 8

Q1: A binary search tree is given by 2 nodes interchanged, find the 2 nodes. my sol : just in-order traveral the tree, it should be ascending sequence.  since there are 2 nodes swapped, scan the sequence, find 2 nodes … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

serialize and deserialize a tree

serialize, ie, convert the object to a byte stream to transfer it through networking, de-seralize, re-construct the object by give output stream. here is the implementation serialize : public static void serialize( OutputStream out, Node node ){    if( node == … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

amazon set 1

Question 3: There is a N*N integer matrix Arr[N][N]. From the row r and   column c, we can go to any of the following three indices:    I.                Arr[ r+1 ][ c-1 ] (valid only if c-1>=0)    II.               Arr[ … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

amazon set 1 answer

amazon set 1 Q1: a sorted array, left rotation r times, find the r in least possible times. sol 1. scan the array, find the min—o(n) sol 2. binary search int findR( int[] arr, int left, int right ) { … Continue reading

Posted in Uncategorized | Tagged | Leave a comment