The document describes a 3-step algorithm to convert a binary tree to a binary search tree while maintaining the original structure of the binary tree. The steps are:
1) Perform an inorder traversal of the binary tree and store the node values in an array. This takes O(n) time.
2) Sort the array. This takes O(nlogn) time using an efficient sorting algorithm like merge sort.
3) Perform another inorder traversal of the binary tree and replace each node's value with the corresponding value from the sorted array. This again takes O(n) time.
1 of 13
Download to read offline
More Related Content
Binary Tree to Binary Search Tree Conversion - GeeksforGeeks