-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathBinaryTrees.c++
116 lines (107 loc) · 3.11 KB
/
BinaryTrees.c++
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*For a given postorder and inorder traversal of a Binary Tree of type integer stored in an array/list, create the binary tree using the given two arrays/lists. You just need to construct the tree and return the root.*/
#include <iostream>
#include <queue>
template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};
using namespace std;
BinaryTreeNode<int>* buildTreeHelper(int *post,int *in,int posts,int poste,int ins,int ine){
if(posts>poste || ins>ine){
return NULL;
}
int rootdata=post[poste];
int rootidx=-1;
for(int i=0;i<ine+1;i++){
if(rootdata==in[i]){
rootidx=i;
break;
}
}
int lposts=posts;
int lins=ins;
int line=rootidx-1;
int lposte=line-ins+posts;
int rins=rootidx+1;
int rine=ine;
int rposte=poste-1;
int rposts=lposte+1;
BinaryTreeNode<int>* root=new BinaryTreeNode<int>(rootdata);
root->left=buildTreeHelper(post,in,lposts,lposte,lins,line);
root->right=buildTreeHelper(post,in,rposts,rposte,rins,rine);
return root;
}
BinaryTreeNode<int>* buildTree(int *postorder, int postLength, int *inorder, int inLength) {
return buildTreeHelper(postorder,inorder,0,postLength-1,0,inLength-1);
}
BinaryTreeNode<int> *takeInput() {
int rootData;
cin >> rootData;
if (rootData == -1) {
return NULL;
}
BinaryTreeNode<int> *root = new BinaryTreeNode<int>(rootData);
queue<BinaryTreeNode<int> *> q;
q.push(root);
while (!q.empty()) {
BinaryTreeNode<int> *currentNode = q.front();
q.pop();
int leftChild, rightChild;
cin >> leftChild;
if (leftChild != -1) {
BinaryTreeNode<int> *leftNode = new BinaryTreeNode<int>(leftChild);
currentNode->left = leftNode;
q.push(leftNode);
}
cin >> rightChild;
if (rightChild != -1) {
BinaryTreeNode<int> *rightNode =
new BinaryTreeNode<int>(rightChild);
currentNode->right = rightNode;
q.push(rightNode);
}
}
return root;
}
void printLevelATNewLine(BinaryTreeNode<int> *root) {
queue<BinaryTreeNode<int> *> q;
q.push(root);
q.push(NULL);
while (!q.empty()) {
BinaryTreeNode<int> *first = q.front();
q.pop();
if (first == NULL) {
if (q.empty()) {
break;
}
cout << endl;
q.push(NULL);
continue;
}
cout << first->data << " ";
if (first->left != NULL) {
q.push(first->left);
}
if (first->right != NULL) {
q.push(first->right);
}
}
}
int main() {
int size;
cin >> size;
int *post = new int[size];
int *in = new int[size];
for (int i = 0; i < size; i++) cin >> post[i];
for (int i = 0; i < size; i++) cin >> in[i];
BinaryTreeNode<int> *root = buildTree(post, size, in, size);
printLevelATNewLine(root);
}