-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconstruct-a-binary-tree-from-postorder-and-inorder.cpp
More file actions
59 lines (50 loc) · 1.26 KB
/
construct-a-binary-tree-from-postorder-and-inorder.cpp
File metadata and controls
59 lines (50 loc) · 1.26 KB
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
// https://www.geeksforgeeks.org/construct-a-binary-tree-from-postorder-and-inorder/
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node *left , *right;
};
Node *getNode(int data){
Node *temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
}
int search(int in[], int data, int l, int r){
for(int i=l ;i<=r; i++){
if(in[i] == data){
return i;
}
}
return -1;
}
Node *buildTree(int in[], int post[], int inStart, int inEnd,int n){
static int postIndex = n-1;
if(inStart > inEnd)
return NULL;
Node *root = getNode(post[postIndex]);
postIndex -= 1;
if(inStart == inEnd)
return root;
int inIndex = search(in,root->data,inStart, inEnd);
if(inIndex == -1){
cout << "Error\n";
return NULL;
}
root->right = buildTree(in, post, inIndex+1, inEnd,n);
root->left = buildTree(in,post,inStart,inIndex-1,n);
return root;
}
void inorder(Node *root){
if(!root) return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
int main(){
int post[] = {8,4,5,2,6,7,3,1};
int in[] = {4,8,2,5,1,6,3,7};
Node *root = buildTree(in,post,0,7,8);
inorder(root);
return 0;
}