forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 39
/
constructBinaryTreeFromInorderAndPostorderTraversal.cpp
162 lines (130 loc) · 4.02 KB
/
constructBinaryTreeFromInorderAndPostorderTraversal.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Source : https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
// Author : Hao Chen
// Date : 2014-07-10
/**********************************************************************************
*
* Given inorder and postorder traversal of a tree, construct the binary tree.
*
* Note:
* You may assume that duplicates do not exist in the tree.
*
*
**********************************************************************************/
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode *buildTree(vector<int> &inorder, int in_offset, vector<int> &postorder, int post_offset, int n );
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return buildTree(inorder, 0, postorder, 0, postorder.size());
}
// n - how many number, offset - start from where?
TreeNode *buildTree(vector<int> &inorder, int in_offset, vector<int> &postorder, int post_offset, int n ) {
if ( n<=0 || postorder.size()<=0 || inorder.size()<=0 ) return NULL;
TreeNode *root = new TreeNode(postorder[post_offset+n-1]);
if ( n==1 ){
return root;
}
//searching in inorder -- can be optimized by using <map>
int i;
for(i=in_offset; i<in_offset+n; i++){
if (inorder[i] == postorder[post_offset+n-1]){
break;
}
}
//error: not found
if (i == inorder.size()) return NULL;
int left_n = i - in_offset;
int right_n = in_offset + n - i - 1;
root->left = buildTree(inorder, in_offset, postorder, post_offset, left_n );
root->right = buildTree(inorder, i+1, postorder, post_offset+left_n, right_n);
return root;
}
//cause the problem: memory limited error
TreeNode *buildTree2(vector<int> &inorder, vector<int> &postorder) {
if (postorder.size()<=0 || inorder.size()<=0 ) return NULL;
int post_n = postorder.size();
TreeNode *root = new TreeNode(postorder[post_n-1]);
if ( inorder.size()==1 && postorder.size()==1 ){
return root;
}
//searching in inorder -- can be optimized by using <map>
int i;
for(i=0; i<inorder.size(); i++){
if (inorder[i] == postorder[post_n-1]){
break;
}
}
//error: not found
if (i == inorder.size()) return NULL;
vector<int> in(inorder.begin(), inorder.begin()+i);
vector<int> post(postorder.begin(), postorder.begin()+i);
if (in.size()>0) {
root->left = buildTree(in, post);
}
in.clear();
in.assign(inorder.begin()+i+1, inorder.end());
post.clear();
post.assign(postorder.begin()+i, postorder.end()-1);
if (in.size()>0) {
root->right = buildTree(in, post);
}
return root;
}
void printTree_pre_order(TreeNode *root)
{
if (root == NULL){
printf("# ");
return;
}
printf("%c ", root->val );
printTree_pre_order(root->left);
printTree_pre_order(root->right);
}
void printTree_in_order(TreeNode *root)
{
if (root == NULL){
printf("# ");
return;
}
printTree_in_order(root->left);
printf("%c ", root->val );
printTree_in_order(root->right);
}
void printTree_level_order(TreeNode *root)
{
queue<TreeNode*> q;
q.push(root);
while (q.size()>0){
TreeNode* n = q.front();
q.pop();
if (n==NULL){
printf("# ");
continue;
}
printf("%c ", n->val);
q.push(n->left);
q.push(n->right);
}
printf("\n");
}
int main()
{
int in_order[]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
int post_order[]={'A', 'C', 'E', 'D', 'B', 'H', 'I', 'G', 'F'};
vector<int> inorder( in_order, in_order + 9 );
vector<int> postorder( post_order, post_order + 9 );
TreeNode* tree = buildTree(inorder, postorder);
printTree_level_order(tree);
printTree_pre_order(tree);
printf("\n");
printTree_in_order(tree);
printf("\n");
return 0;
}