Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree.
Example 1 Input: 10 / \ 2 7 / \ 8 4 Output: 8 / \ 4 10 / \ 2 7 Example 2 Input: 10 / \ 30 15 / \ 20 5 Output: 15 / \ 10 20 / \ 5 30
Following is a 3 step solution for converting Binary tree to Binary Search Tree.
1) Create a temp array arr that stores inorder traversal of the tree. This step takes O(n) time.
2) Sort the temp array arr. Time complexity of this step depends upon the sorting algorithm. In the following implementation, Quick Sort is used which takes (n^2) time. This can be done in O(nLogn) time using Heap Sort or Merge Sort.
3) Again do inorder traversal of tree and copy array elements to tree nodes one by one. This step takes O(n) time.
Following is C implementation of the above approach. The main function to convert is highlighted in the following code.
Following is Inorder Traversal of the converted BST: 5 10 15 20 30
We will be covering another method for this problem which converts the tree using O(height of tree) extra space.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- K'th Largest Element in BST when modification to BST is not allowed
- Transform a BST to greater sum tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Find a pair with given sum in a Balanced BST
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Construct BST from given preorder traversal | Set 2
- Construct BST from given preorder traversal | Set 1
- Two nodes of a BST are swapped, correct the BST
- Check if each internal node of a BST has exactly one child
- Merge two BSTs with limited extra space
- Merge Two Balanced Binary Search Trees
- Sorted Array to Balanced BST
- Sorted Linked List to Balanced BST
- Total number of possible Binary Search Trees and Binary Trees with n keys
- A program to check if a binary tree is BST or not