Here is a question I was recently asked in an interview. A binary tree is given with a condition that each left child is 1 smaller than root and right child is 1 larger. Here is a sample tree
Sort it in O(1) and O(n) time complexity.
Here are the approaches I suggested:
What are your approaches?
Fix some Leaf node of the given tree as NewHead .
Write a function Pop() remove some node from the given tree ..!
Write pop node such that you will remove it only when it is ! equal to NewHead .
So pop value from tree , insert it to the New binary search tree with New Head as Head node .
So u will remove an element from tree add it to new search tree .
Until tree head point NewHead .
So all you elements are now in binary search tree pointing to the New head , which will be
obviously in sorted order .
This way promise you a sorting in O(NlogN) .