-
Notifications
You must be signed in to change notification settings - Fork 1
/
Preorder_to_Postorder.cpp
74 lines (62 loc) · 1.6 KB
/
Preorder_to_Postorder.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
/*
Problem Statement:
------------------
Given an array arr[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal.
Example 1:
---------
Input:
N = 5
arr[] = {40,30,35,80,100}
Output: 35 30 100 80 40
Explanation: PreOrder: 40 30 35 80 100. InOrder: 30 35 40 80 100.
Therefore, the BST will be:
40
/ \
30 80
\ \
35 100
Hence, the postOrder traversal will
be: 35 30 100 80 40
Example 2:
---------
Input:
N = 8
arr[] = {40,30,32,35,80,90,100,120}
Output: 35 32 30 120 100 90 80 40
Your Task:
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.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
*/
// Link --> https://practice.geeksforgeeks.org/problems/preorder-to-postorder4423/1
// Code:
Node* constructTree(int pre[] , int size)
{
if(canRepresentBST(pre , size) == false)
{
cout<<"NO";
return NULL;
}
Stack* stack = createStack(size);
Node* root = newNode(pre[0]);
push(stack , root);
int i;
Node* temp;
for(i=1 ; i<size ; i++)
{
temp = NULL;
while(!isEmpty(stack) && pre[i] > peek(stack)->data)
temp = pop(stack);
if(temp != NULL)
{
temp->right = newNode(pre[i]);
push(stack , temp->right);
}
else
{
peek(stack)->left = newNode(pre[i]);
push(stack , peek(stack)->left);
}
}
return root;
}