-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathBinary Tree to BST.cpp
67 lines (55 loc) · 1.23 KB
/
Binary Tree to BST.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Binary Tree to BST
==================
Given a Binary Tree, convert it to Binary Search Tree in such a way that keeps the original structure of Binary Tree intact.
Example 1:
Input:
1
/ \
2 3
Output: 1 2 3
Example 2:
Input:
1
/ \
2 3
/
4
Output: 1 2 3 4
Explanation:
The converted BST will be
3
/ \
2 4
/
1
Your Task:
You don't need to read input or print anything. Your task is to complete the function binaryTreeToBST() which takes the root of the Binary tree as input and returns the root of the BST. The driver code will print inorder traversal of the converted BST.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
*/
void inorder(Node *root, vector<int> &ans)
{
if (!root)
return;
inorder(root->left, ans);
ans.push_back(root->data);
inorder(root->right, ans);
}
void replaceValues(Node *&root, vector<int> &in, int &i)
{
if (!root)
return;
replaceValues(root->left, in, i);
root->data = in[i++];
replaceValues(root->right, in, i);
}
Node *binaryTreeToBST(Node *root)
{
vector<int> in;
inorder(root, in);
sort(in.begin(), in.end());
int i = 0;
replaceValues(root, in, i);
return root;
}