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.
This solution will use Sets of C++ STL instead of array based solution.
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
- Copy the items of binary tree in a set while doing inorder traversal. This takes O(n log n) time. Note that set in C++ STL is implemented using a Self Balancing Binary Search Tree like Red Black Tree, AVL Tree, etc
- There is no need to sort the set as sets in C++ are implemented using Self-balancing binary search trees due to which each operation such as insertion, searching, deletion etc takes O(log n) time.
- Now simply copy the items of set one by one from beginning to the tree while doing inorder traversal of tree. Care should be taken as when copying each item of set from its beginning, we first copy it to the tree while doing inorder traversal, then remove it from the set as well.
Now the above solution is simpler and easier to implement than the array based conversion of Binary tree to Binary search tree explained here- Conversion of Binary Tree to Binary Search tree (Set-1), where we had to separately make a function to sort the items of the array after copying the items from tree to it.
Program to convert a binary tree to binary search tree using set.
Inorder traversal of BST is: 1 5 6 7 9 10 11
Time Complexity: O(n Log n)
Auxiliary Space: (n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Binary Tree to Binary Search Tree Conversion
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Difference between Binary Tree and Binary Search Tree
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Count the Number of Binary Search Trees present in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Find the node with minimum value in a Binary Search Tree using recursion
- Find the node with maximum value in a Binary Search Tree using recursion
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Binary Tree | Set 3 (Types of Binary Tree)
- Find the node with minimum value in a Binary Search Tree
- Lowest Common Ancestor in a Binary Search Tree.
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Construct a Binary Search Tree from given postorder
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.