-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebuildTree.cpp
More file actions
93 lines (82 loc) · 2.08 KB
/
Copy pathrebuildTree.cpp
File metadata and controls
93 lines (82 loc) · 2.08 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
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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Node{
public:
int data;
Node * left;
Node * right;
Node(int val): data(val),left(NULL), right(NULL){};
Node (): data(0), left(NULL), right(NULL){};
};
Node * buildTree(vector<int>in,int inStart,int inEnd,
vector<int> pre,int preStart,int preEnd)
{
if(inStart>inEnd) return NULL;
int val = pre[preStart];
Node * root = new Node(val);
// find the position of root in inOrderArray
int posOfRoot =0;
for( int ii=inStart; ii<=inEnd; ii++){
if(in[ii] == val){
posOfRoot = ii;
break;
}
}
int index = posOfRoot - inStart;
root->left = buildTree(in, inStart, (posOfRoot-1),
pre, (preStart+1), index-1);
root->right = buildTree(in, (posOfRoot+1), inEnd,
pre, preStart + index+1, preEnd);
return root;
}
void InOrder( Node * node){
if(node== NULL) return;
InOrder(node->left);
cout << node->data << " ";
InOrder(node->right);
}
void PrintLineOrder(Node * root){
if(root == NULL) return;
queue<Node*> currentLevel, nextLevel;
currentLevel.push(root);
while(!currentLevel.empty()){
Node * curNode = currentLevel.front();
currentLevel.pop();
if(curNode){
cout << curNode->data << " ";
nextLevel.push(curNode->left);
nextLevel.push(curNode->right);
}
if(currentLevel.empty()){
cout << "\n";
queue<Node*> temp = currentLevel;
currentLevel=nextLevel;
nextLevel = temp;
}
}
}
void constrctTree(vector<int> iInOrderArray, vector<int> iPreOrderArray){
int size = iInOrderArray.size()-1;
Node * root = buildTree(iInOrderArray, 0, size, iPreOrderArray, 0, size);
cout << "\n Line Order \n";
PrintLineOrder(root);
}
int main (){
int n;
int num;
cin>> n;
vector<int> inOrderArr;
for (int i =0; i<n; i++){
cin>> num;
inOrderArr.push_back(num);
}
vector<int> preOrderArr;
for (int i =0; i<n; i++){
cin>> num;
preOrderArr.push_back(num);
}
constrctTree(inOrderArr, preOrderArr);
return 0;
}