-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path1a_Traverse_Recursive.cpp
81 lines (71 loc) · 1.45 KB
/
1a_Traverse_Recursive.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
/*
2019-12.1
二叉树的遍历
输入:
3 1
1 2 3
2 0 0
3 0 0
输出:
1 2 3
2 1 3
2 3 1
*/
#include <iostream>
using namespace std;
struct Node
{
int val;
Node *left, *right;
};
Node* Construct() // 构造二叉树
{
// 输入格式: 节点个数+头结点编号
// 每行对应当前节点的编号、左右儿子, 0表示是空节点
int n, root_ind;
cin >> n >> root_ind;
Node *node = new Node[n+1]; // 注意默认构造函数问题!
int fa, lch, rch;
for (int i = 1; i <= n; i++)
{
cin >> fa >> lch >> rch;
node[fa].val = fa;
node[fa].left = lch!=0 ? &node[lch] : NULL; // 是0则为NULL,否则指向lch的地址
node[fa].right = rch!=0 ? &node[rch] : NULL;
}
return &node[root_ind]; // 返回指针
}
void PreOrder(Node* root) // 先序:中左右
{
if (root == NULL)
return ;
cout << root->val << " ";
PreOrder(root->left);
PreOrder(root->right);
}
void InOrder(Node* root) // 中序:左中右
{
if (root == NULL)
return ;
InOrder(root->left);
cout << root->val << " ";
InOrder(root->right);
}
void PosOrder(Node* root) // 后序:左右中
{
if (root == NULL)
return ;
PosOrder(root->left);
PosOrder(root->right);
cout << root->val << " ";
}
int main()
{
Node *root = Construct(); // 构建二叉树
PreOrder(root);
cout << endl;
InOrder(root);
cout << endl;
PosOrder(root);
return 0;
}