Binary Tree to BST
For a given a binary tree, converts the binary tree to binary search tree(BST) and returns the root of the converted binary search tree.
Note : The conversion must be done in such a way that keeps the original structure of Binary Tree.
Solution:
- Traverse array in InOrder fashion and store node value in array
- Sort array
- Again traverse tree and replace node with array
Latest Source Code:
Github: BinaryTreeToBSTConverter.java
Output:
Actual Tree 10 / \ / \ 2 7 / \ 8 4 BST : 8 / \ / \ 4 10 / \ 2 7