-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy path10.4.cpp
More file actions
78 lines (70 loc) · 1.98 KB
/
10.4.cpp
File metadata and controls
78 lines (70 loc) · 1.98 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
/*
* 题目名称:二叉排序树
* 题目来源:华中科技大学复试上机题
* 题目链接:http://t.cn/AiKD0L5V
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
using namespace std;
struct TreeNode {
int data;
TreeNode* leftChild;
TreeNode* rightChild;
TreeNode(int x): data(x), leftChild(NULL), rightChild(NULL) {}
};
TreeNode* Insert(TreeNode* root, int x) {
if (root == NULL) { //创建新节点
root = new TreeNode(x);
} else if (x < root->data) { //插入左子树
root->leftChild = Insert(root->leftChild, x);
} else if (root->data < x) { //插入右子树
root->rightChild = Insert(root->rightChild, x);
}
return root;
}
void PreOrder(TreeNode* root) { //前序遍历
if (root == NULL) {
return;
}
printf("%d ", root->data);
PreOrder(root->leftChild);
PreOrder(root->rightChild);
return;
}
void InOrder(TreeNode* root) { //中序遍历
if (root == NULL) {
return;
}
InOrder(root->leftChild);
printf("%d ", root->data);
InOrder(root->rightChild);
return;
}
void PostOrder(TreeNode* root) { //后序遍历
if (root == NULL) {
return;
}
PostOrder(root->leftChild);
PostOrder(root->rightChild);
printf("%d ", root->data);
return;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
TreeNode* root = NULL; //建立空树
for (int i = 0; i < n; ++i) { //逐个插入
int x;
scanf("%d", &x);
root = Insert(root, x);
}
PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
}
return 0;
}