-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_Sep_2023_Binary_tree_to_BST.java
47 lines (40 loc) · 1.35 KB
/
8_Sep_2023_Binary_tree_to_BST.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Problem Link: https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1
Problem Statement: Given a Binary Tree, convert it to Binary Search Tree in such a way that keeps the original structure of Binary Tree intact.
Solution Approach: Create the list from the given binary tree, and sort this, now traverse it and keep updating the
values in the original Binary tree to make it a BST.
*/
/* ------------CODE---------------- */
class Solution
{
// The given root is the root of the Binary Tree
// Return the root of the generated BST
List<Integer> list;
int index = 0;
Node binaryTreeToBST(Node root)
{
list = new ArrayList<>();
traverse(root, false); // to fill the list with all the values
// now sort this list
Collections.sort(list);
// now again do inorder traversal but this time update the node's value with the one in the list
traverse(root, true);
return root;
}
private void traverse(Node root, boolean isUpdate) {
if(root==null)
return;
traverse(root.left, isUpdate);
if(isUpdate) {
root.data = list.get(index++);
}
else {
list.add(root.data);
}
traverse(root.right, isUpdate);
}
}
/*
Time Complexity: O(n)
Space Complexity: O(n)
*/