-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinary_Tree_to_BST.cpp
81 lines (65 loc) · 1.51 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
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.
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).
*/
// Link --> https://practice.geeksforgeeks.org/problems/binary-tree-to-bst/1#
// Code:
class Solution
{
public:
vector <int> in;
void preorder(Node *root)
{
if(root == NULL)
return;
in.push_back(root->data);
preorder(root->left);
preorder(root->right);
}
int index = 0;
void BST(Node *root)
{
if(root == NULL)
return;
BST(root->left);
root->data = in[index++];
BST(root->right);
}
Node* binaryTreeToBST(Node *root)
{
if(root == NULL)
return NULL;
preorder(root);
sort(in.begin() , in.end());
BST(root);
return root;
}
};