-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeUsingPostorderAndInorder.cpp
More file actions
54 lines (49 loc) · 1.09 KB
/
binaryTreeUsingPostorderAndInorder.cpp
File metadata and controls
54 lines (49 loc) · 1.09 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
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *left, *right;
node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
int search(int arr[], int l, int h, int target)
{
for (int i = l; i <= h; i++)
if (arr[i] == target)
return i;
return -1;
}
node *buildTree(int postorder[], int inorder[], int start, int end)
{
static int idx = end;
if (start > end)
return NULL;
node *root = new node(postorder[idx--]);
if (start == end)
return root;
int pos = search(inorder, start, end, root->data);
root->right = buildTree(postorder, inorder, pos + 1, end);
root->left = buildTree(postorder, inorder, start, pos - 1);
return root;
}
void printInOrder(node *root)
{
if (root == NULL)
return;
printInOrder(root->left);
cout << root->data << " ";
printInOrder(root->right);
}
int main()
{
int postorder[] = {4, 2, 5, 3, 1};
int inorder[] = {4, 2, 1, 5, 3};
node *root = buildTree(postorder, inorder, 0, 4);
printInOrder(root);
}