forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructBinaryTreeFromPreorderAndInorderTraversal.cpp
134 lines (108 loc) · 2.95 KB
/
constructBinaryTreeFromPreorderAndInorderTraversal.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
// Source : https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
// Author : Hao Chen
// Date : 2014-07-09
/**********************************************************************************
*
* Given preorder and inorder 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>& preorder, int& preidx, vector<int>& inorder);
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
int preidx=0;
return buildTree(preorder, preidx, inorder);
}
TreeNode *buildTree(vector<int>& preorder, int& preidx, vector<int>& inorder) {
if (preorder.size()<=0 || inorder.size()<=0 ) return NULL;
TreeNode *root = new TreeNode(preorder[preidx]);
if (inorder.size()==1){
return root;
}
int i;
for(i=0; i<inorder.size(); i++){
if (inorder[i] == preorder[preidx]){
break;
}
}
//error: not found
if (i == inorder.size()) return NULL;
if (preidx+1 >= preorder.size()){
return root;
}
vector<int> v(inorder.begin(), inorder.begin()+i);
if (v.size()>0) {
preidx++;
root->left = buildTree(preorder, preidx, v);
}
v.clear();
v.assign(inorder.begin()+i+1, inorder.end());
if (v.size()>0) {
preidx++;
root->right = buildTree(preorder, preidx, v);
}
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 pre_order[]={'F', 'B', 'A', 'D', 'C', 'E', 'G', 'I', 'H'};
int in_order[]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
vector<int> preorder( pre_order, pre_order + 9 );
vector<int> inorder( in_order, in_order + 9 );
TreeNode* tree = buildTree(preorder, inorder);
printTree_level_order(tree);
printTree_pre_order(tree);
printf("\n");
printTree_in_order(tree);
printf("\n");
return 0;
}