GeeksforGeeks » Interview Questions
Google Interview Question for Software Engineer/Developer (Fresher)
(2 posts)-
convert sorted doubly link list ->BST
-
You must use the following algorithm to build the search tree:
1. Find the middle node, build a root node (TNode) with an info field the same as the middle node
2. Unlink the middle node from the list (so the next field of the node before it points to NULL, and the prev field of the node after it points to NULL) --- this step is necessary for the next step (so that MiddleNode will work) --- you may want to store pointers to the nodes before and after the middle node.
3. Make left and right subtrees recursively, using the list before the middle node for the left subtree and the list after the middle node for the right subtree.
4. Link the middle node back into the list (which is why you may have stored pointers in step 2).
Reply
You must log in to post.