How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

bit-question picture bit-question · Apr 5, 2010 · Viewed 17.1k times · Source

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

Answer

Peter picture Peter · Aug 29, 2012

Convert Binary Tree to a doubly linked list- can be done inplace in O(n)
Then sort it using merge sort, nlogn
Convert the list back to a tree - O(n)

Simple nlogn solution.