Skip to content

Commit 42f0336

Browse files
committed
Preorder to BST
1 parent 83a3b38 commit 42f0336

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@
219219
- [Count BST nodes that lie in a given range](https://practice.geeksforgeeks.org/problems/count-bst-nodes-that-lie-in-a-given-range/1# "view question") - [Cpp Solution](./solutions/Count%20BST%20nodes%20that%20lie%20in%20a%20given%20range.cpp)
220220
- [Replace every element with the least greater element on its right](https://www.geeksforgeeks.org/replace-every-element-with-the-least-greater-element-on-its-right/ "view question")
221221
- []( "view question") - [Cpp Solution](./solutions/.cpp)
222+
- [Preorder to BST](https://practice.geeksforgeeks.org/problems/preorder-to-postorder4423/1# "view question") - [Cpp Solution](./solutions/Preorder%20to%20BST.cpp)
223+
- []( "view question") - [Cpp Solution](./solutions/.cpp)
222224

223225
### Greedy Method
224226

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Preorder to BST
3+
===============
4+
5+
Given an array arr[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal.
6+
7+
Example 1:
8+
Input:
9+
N = 5
10+
arr[] = {40,30,35,80,100}
11+
Output: 35 30 100 80 40
12+
Explanation: PreOrder: 40 30 35 80 100
13+
InOrder: 30 35 40 80 100
14+
Therefore, the BST will be:
15+
40
16+
/ \
17+
30 80
18+
\ \
19+
35 100
20+
Hence, the postOrder traversal will
21+
be: 35 30 100 80 40
22+
23+
Example 2:
24+
Input:
25+
N = 8
26+
arr[] = {40,30,32,35,80,90,100,120}
27+
Output: 35 32 30 120 100 90 80 40
28+
Your Task:
29+
You need to complete the given function and return the root of the tree. The driver code will then use this root to print the post order traversal.
30+
31+
Expected Time Complexity: O(N).
32+
Expected Auxiliary Space: O(N).
33+
34+
Constraints:
35+
1 <= N <= 103
36+
1 <= arr[i] <= 104
37+
*/
38+
39+
Node *build(int pre[], int s, int e, unordered_map<int, int> &next_greater)
40+
{
41+
if (s > e)
42+
return NULL;
43+
if (s == e)
44+
{
45+
Node *root = new Node();
46+
root->data = pre[s];
47+
return root;
48+
}
49+
50+
Node *root = new Node();
51+
root->data = pre[s];
52+
53+
int lb = next_greater[s];
54+
55+
root->left = build(pre, s + 1, lb - 1, next_greater);
56+
root->right = build(pre, lb, e, next_greater);
57+
return root;
58+
}
59+
60+
//Function that constructs BST from its preorder traversal.
61+
Node *constructTree(int pre[], int n)
62+
{
63+
stack<int> st;
64+
unordered_map<int, int> next_greater;
65+
next_greater[n - 1] = n;
66+
st.push(n - 1);
67+
68+
for (int i = n - 2; i >= 0; --i)
69+
{
70+
while (st.size() > 0 && pre[st.top()] <= pre[i])
71+
st.pop();
72+
next_greater[i] = n;
73+
if (st.size())
74+
next_greater[i] = st.top();
75+
st.push(i);
76+
}
77+
78+
// for(auto &i: next_greater) cout<<i.first<<" "<<i.second<<endl;
79+
return build(pre, 0, n - 1, next_greater);
80+
}

0 commit comments

Comments
 (0)