-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path2_Print_Edge_Node.cpp
118 lines (106 loc) · 3.04 KB
/
2_Print_Edge_Node.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
/*
2019-12.15
打印二叉树边界节点
输入:
太多
输出:
1 2 4 7 11 13 14 15 16 12 10 6 3
1 2 4 7 13 14 15 16 10 6 3
*/
#include <iostream>
#include <cstring>
using namespace std;
struct Node
{
int val;
Node *left, *right;
};
Node* Construct() // 构造二叉树
{
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]; // 返回指针
}
const int maxn = 1000005;
int edgemap[maxn][2];
int height = -1; // 树的高度
// 标准一
void setEdgeMap1(Node* node, int h, int edgemap[][2]) // 类似于先序遍历
{
if (node == NULL)
{
if (h > height)
height = h;
return ;
}
edgemap[h][0] = edgemap[h][0]==0? node->val : edgemap[h][0];
edgemap[h][1] = node->val;
setEdgeMap1(node->left, h+1, edgemap);
setEdgeMap1(node->right, h+1, edgemap);
}
// 标准二
void setEdgeMap2Left(Node* node, int h, int edgemap[][2]) // 类似于先序遍历
{
if (!node->left && !node->right)
return ;
edgemap[h][0] = node->val;
if (node->left)
setEdgeMap2Left(node->left, h+1, edgemap);
else
setEdgeMap2Left(node->right, h+1, edgemap);
}
void setEdgeMap2Right(Node* node, int h, int edgemap[][2])
{
if (!node->left && !node->right)
return ;
edgemap[h][1] = node->val;
if (node->right)
setEdgeMap2Right(node->right, h+1, edgemap);
else
setEdgeMap2Right(node->left, h+1, edgemap);
}
// 打印叶子节点(不包括最左/右)
void printLeaf(Node* node, int h, int edgemap[][2])
{
if (node == NULL)
return ;
if (!node->left && !node->right && edgemap[h][0] != node->val && edgemap[h][1] != node->val)
cout << node->val << " ";
printLeaf(node->left, h+1, edgemap);
printLeaf(node->right, h+1, edgemap);
}
int main()
{
Node *root = Construct(); // 构建二叉树
memset(edgemap, 0, sizeof(edgemap));
/* ----------------- 标准一 -------------------*/
setEdgeMap1(root, 0, edgemap); // 寻找边界点
for (int i = 0; i < height; i++) // 打印左边界点
cout << edgemap[i][0] << " ";
printLeaf(root, 0, edgemap); // 打印叶子节点
for (int i = height-1; i >= 0; i--)
if (edgemap[i][0] != edgemap[i][1]) // 打印右边界点
cout << edgemap[i][1] << " ";
cout << endl;
/* ----------------- 标准二 -------------------*/
memset(edgemap, 0, sizeof(edgemap));
setEdgeMap2Left(root, 0, edgemap); // 构建左右两边
setEdgeMap2Right(root, 0, edgemap);
for (int i = 0; i < height; i++)
if (edgemap[i][0] != 0)
cout << edgemap[i][0] << " ";
printLeaf(root, 0, edgemap);
for (int i = height-1; i >= 0; i--)
if (edgemap[i][0] != edgemap[i][1] && edgemap[i][1] != 0)
cout << edgemap[i][1] << " ";
return 0;
}