Monday, October 31, 2011

Given a node of a BST, modify it in such a way that the given node becomes the root. The tree should still be BST.


2 comments :

Karthick said...

Assuming that every node has parent pointer, we can perform left or right rotation(depending on whether the node is right or left child)until we reach the root.

Is this correct?

sreekar said...

Perform an inorder traversal and find the given node in it and keep the given node as root and insert the nodes to it in the given inorder traversal.

This will take O(N log N)

I believe there will be a better solution similar to AVL Tree rotation