Google Interview Question for Developer Program Engineers






Comment hidden because of low score. Click to expand.
4
of 4 vote

What about converting the tree into Double link list , sort the list then make it BST ?

- Coder February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we have to ask if we have to keep the same structure of the tree. if yes.. this technique wont work.. but yes you can flatten the tree. ( link lint) sort it ..and then convert into bst.. in fact .. sorted link list is a bst. ;) ( which is not balanced one)

- deb.abhishek April 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

FOR i = 0; i < hightOfTree
    FOR each node
         IF Predecessor contains greater value in Inorder Traversal swap the values
    For each node
         IF Successor contains lesser value in Inorder Traversal swap the values

- Tulley February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain how and why this algorithm works.
Thanks

- mytestaccount2 August 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This approach wont work for the given tree.
10
/ \
30 15
/ \
20 5

- Isha March 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a procedure similar to heapify in a bottom-up fashion as in build-heap.

- Anonymous February 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

remember its a tree here not an array. heapify relies on array index where direct access to parent is available.

Another things is this approach may not work even if we forget the indexs for eg consider binary tree

50
     /
    60
   /  \ 
  30  40

your algo to convert it to BST may not work.

- mike February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ignore the above comment. It doesn't work in all the cases.

- simil February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the In order traversal comes out as sorted, then the BT is BST
So We can perform a inorder Morris traversal of the tree (inplace traversal), comparing the inorder predessor with current element and current element with inorder successor.
If they dont meet the order, swap them (have an indicator that the elements are swapped).
Perform this repeatedly until no swaps are required.

- Anonymous February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried to code the same..dint compile/test tough


#define swap_data(a,b) {int temp; temp=a->data;a->data=b->data;b->data=temp;}

typedef struct node{
struct node *left;
struct node *right;
int data;
} Node;

void convertBTtoBSTinplace(Node *root)
{

bool swapDone=true;
Node *curNode=NULL;
Node *inOrdPred=NULL;

while(swapDone) {
swapDone=false;
curNode=root;
while (curNode) {
if(curNode->left) {
inOrdPred=curNode->left;
while(inOrdPred->right && inOrdPred->right!=curNode)
inOrdPred=inOrdPred->right;
if (inOrdPred->right==curNode) {
inOrdPred->right=NULL;
if(inOrdPred->data > curNode->data) {
swap_data(inOrdPred,curNode);
swapDone=true;
}
if (curNode->right && curNode->data > curNode->right->data) {
swap_data(curNode,curNode->right);
swapDone=true;
}
curNode=curNode->right;
} else {
if(inOrdPred->data > curNode->data) {
swap_data(inOrdPred,curNode);
swapDone=true;
}
inOrdPred->right=curNode;
curNode=curNode->left;
}
} else {
if (curNode->right && curNode->data > curNode->right->data) {
swap_data(curNode,curNode->right);
swapDone=true;
}
curNode=curNode->right;
}

}
}

}

- R February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it works, I coded in a recursive way that seems to be clearer, and tested on some cases.

Node* 
mytraversal(Node *pred, Node *curNode, int& swap) {
        if(curNode == NULL)
                return 0;
        Node *p = mytraversal(pred, curNode->left, swap);
        if(!p) p = pred;
        if(p && p->value > curNode->value) {
                curNode->value ^= p->value;
                p->value ^= curNode->value;
                curNode->value ^= p->value;
                swap++;
        }
        p = mytraversal(curNode, curNode->right, swap);
        return (p?p:curNode);
}
void
BT2BST(Node *root) {
        int swap;
        do {
                swap = 0;
                mytraversal(NULL, root, swap);
        } while(swap);
}

Just like bubble sort, but it's done on a binary tree.

- Anonymous March 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

or run the post order traversal and send each node to a function to construct a bst

- coder February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

question says inplace. So no extra space to be used.

- Anonymous February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This might not be the best way but works.
Convert the BT to a linked list in place. Construct a BST by inserting each node of the linked list. No extra space required.

- Anon February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this, disassemble the old tree and assemble the new BST in pre-order.
1. Keep the original root as the new root.
2. From the current node do a pre-order traverse. Each time insert the current node into the new BST.
3. Then recursively disassemble the left tree and the right tree to insert into the new BFT.
It is inplace and preorder traverse is O(n), assambling the new BFT is O(nlogn). So, totaly time complexity is O(nlogn).

- Anonymous February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tree* makeBst:
if(!this) return NULL;
tree *x = left->makeBst();
tree* y = right->makeBst();
left = right = 0;
x=x->insert(this);
return merge(x,y);

- Anonymous March 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tree* makeBst:
if(!this) return NULL;
tree *x = left->makeBst();
tree* y = right->makeBst();
left = right = 0;
x=x->insert(this);
return merge(x,y);

- Anonymous March 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here, the idea is to add leaf nodes to the search tree recursively. Best way is to do a post-order traversal and add your children to BST.

add_to_bst(node*& root, node* newnode)
{
   if(!root) root = ptr;
   else if(root->key < newnode->key)
      add_to_bst(root->right, newnode);
   else
      add_to_bst(root->left, newnode);
}

void make_bst(node* currnode, node*& bstroot)
{
   if(!currnode) return;
   postorder(currnode->left);
   postorder(currnode->right);
   add_to_bst(bstroot, currnode->left);
   add_to_bst(bstroot, currnode->right);
   ptr->left = 0;
   ptr->right = 0;
}

// just call
node* bt_root = input;
node* bst_root = 0;
make_bst(bt_root, bst_root);
// don't forget to add the root node to bst
add_to_bst(bst_root,bt_root);

- Anonymous March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, read postorder() as make_bst() and 'ptr' as 'currnode'. I forgot to change names. sorry again.

- Anonymous March 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, read postorder() as make_bst() and 'ptr' as 'currnode'. I forgot to change names. sorry again.

- Anonymous March 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think recursive solution are not in-place. It will implicitly use O(logn) stack space. Maybe the interviewer allow this...

- Anonymous March 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// do a post order traversal
// at root, if left subtree's data is greater than right subtree's data, swap both subtrees

// if root data is less than left subtree's data then swap root data with left subtree's data and go on doing this untill root's data is greater than left subtree's data

// similar for right subtree

Here is the code. Recursion will have a cost O(n) worst case space complexity

// inplace conversion of a binary tree to BST
void tree_to_bst(Tree *root)
{
if( !root ||
!root->left ||
!root->right )
return;

tree_to_bst(root->left);
tree_to_bst(root->right);

// if left subtree is greater than right subtree
if(root->left && root->right && root->left->data > root->right->data)
swap<Tree*>(root->left, root->right);

// check if root is lesser than left subtrees
if(root->left && root->data < root->left->data)
{
do
{
swap<int>(root->data, root->left->data);
root = root->left;
}
while(root->left && root->data < root->left->data);
}
// check if root is greater than right subtrees
else if(root->right && root->data > root->right->data)
{
do
{
swap<int>(root->data, root->right->data);
root = root->right;
}while(root->right && root->data > root->right->data);
}
}

- Hji March 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http : / / geeksforgeeks . org / forum / topic / binary-tree-to-bst

- Anonymous July 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http : / / www . ritambhara . in / convert-a-binary-tree-to-binary-search-tree/

- kamal August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. Convert the left-subtree.
2. Convert the right-subtree.
3. if(the root is lessthan its predecessor) {
swap root with its predecessor and apply heapify on it
} else if(the root is greaterthan its successor) {
swap root with its successor and apply heapify on it
} else {
//it is already a BST. Do nothing
}
Note: By Predecessor i mean the right-most node of the node's left subtree and By Successor it is the left-most node in the node's right subtree

- simil February 23, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More