-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq1.cpp
More file actions
81 lines (66 loc) · 1.72 KB
/
Copy pathq1.cpp
File metadata and controls
81 lines (66 loc) · 1.72 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
#include <iostream>
#include <vector>
using namespace std;
// 1. 由中根序列和后根序列重建二叉树
// 后根序列的最后一个元素是根结点,中根序列的最后一个元素是根结点的右子结点
// 中根序列={左子树的中根序列+根结点+右子树的中根序列}
// 后跟序列={左子树的后根序列+右子树的后根序列+根结点}
struct BinaryTreeNode {
int data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(int x) : data(x), left(NULL), right(NULL) {}
};
int inOrder[65536];
int postOrder[65536];
BinaryTreeNode* buildTree(int inBegin, int inEnd, int postBegin, int postEnd) {
// 找到根结点
BinaryTreeNode* root = new BinaryTreeNode(postOrder[postEnd]);
// 寻找左右子树的分割点
int split = 0;
for (split = inBegin; split <= inEnd; split++) {
if (root->data == inOrder[split]) {
break;
}
}
// 左子树序列长度
int lLength = split - inBegin;
// 右子树序列长度
int rLength = inEnd - split;
// 左子树不为空
if (lLength > 0) {
root->left = buildTree(inBegin, split - 1, postBegin, postBegin + lLength - 1);
}
// 右子树不为空
if (rLength > 0) {
root->right = buildTree(split + 1, inEnd, postBegin + lLength, postEnd - 1);
}
return root;
}
void preOrderTraverse(BinaryTreeNode* T) {
if (T == NULL) {
return;
}
cout << T->data << " ";
preOrderTraverse(T->left);
preOrderTraverse(T->right);
}
int main(int argc, char *argv[]) {
int n = 0;
int value = 0;
// 中根序列
while (cin >> inOrder[n++]) {
if (cin.get() != ' ') {
break;
}
};
n = 0;
// 后根序列
while (cin >> postOrder[n++]) {
if (cin.get() != ' ') {
break;
}
}
preOrderTraverse(buildTree(0, n - 1, 0, n - 1));
return 0;
}