Epic Systems Interview Question
Software Engineer / DevelopersWill this work?
Do a depth first search on the binary tree and every time a node is complete (all the children are traversed), detach that from the tree and start creating a binary search tree (insert function)
For every node you found during the depth first search which has null, you can safely detach that from the current tree and use that object reference to add to a binary search tree... Every node you will be removing will always have nulls as their children because you have removed them already...
Since the question mentions it is to be done in place; we can't use another data structure.
My idea - Do a BFS using a queue. Whenever you visit a node; check left and right children. Put the middle value among them at the place of current node, smallest at the left and largest at right. (BST concept)
Do it n times so that every element would be at proper place.
Time complexity - O(n)
Space complexity - O(1)
Cmon...does epic ask these questions...u must be jokin..
- Anon September 19, 2009