-
Notifications
You must be signed in to change notification settings - Fork 0
/
offer23.cpp
156 lines (144 loc) · 3.27 KB
/
offer23.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*****************************************************************************************************
* 剑指offer第23题
* 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
*
* Input: 二叉树
* Output: 二叉树的广度优先遍历序列
*
* Note:
* 利用辅助队列实现广度优先遍历。(不管是树或者有向图都用队列实现)
1.将起始点放到队列中,
2.接下来每一次从队列的头部取出一个节点,遍历这个节点之后把从它能到达的节点都一次放到队列
3.重复遍历过程,直到队列中的节点全部被遍历为止
* author: [email protected]
* time: 2019.5.6
******************************************************************************************************/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
};
int num;
void CreateTree(TreeNode **root, int n)
{
int *num = new int[n], i, tmp;
char child_num;
TreeNode *index, *tmpNode;
queue<TreeNode *> Bqueue;
if (n <= 0)
{
*root = NULL;
return;
}
*root = new TreeNode;
(*root)->left = NULL;
(*root)->right = NULL;
for (i = 0; i < n; i++)
cin >> num[i];
(*root)->value = num[0];
Bqueue.push(*root);
for (i = 0; i < n; i++)
{
cin >> child_num;
index = Bqueue.front();
Bqueue.pop();
if (child_num == 'd')
{
cin >> tmp;
tmpNode = new TreeNode;
tmpNode->value = num[tmp - 1];
tmpNode->left = NULL;
tmpNode->right = NULL;
index->left = tmpNode;
Bqueue.push(tmpNode);
cin >> tmp;
tmpNode = new TreeNode;
tmpNode->value = num[tmp - 1];
tmpNode->left = NULL;
tmpNode->right = NULL;
index->right = tmpNode;
Bqueue.push(tmpNode);
}
else if (child_num == 'l')
{
cin >> tmp;
tmpNode = new TreeNode;
tmpNode->value = num[tmp - 1];
tmpNode->left = NULL;
tmpNode->right = NULL;
index->left = tmpNode;
Bqueue.push(tmpNode);
}
else if (child_num == 'r')
{
cin >> tmp;
tmpNode = new TreeNode;
tmpNode->value = num[tmp - 1];
tmpNode->right = NULL;
tmpNode->left = NULL;
index->right = tmpNode;
Bqueue.push(tmpNode);
}
if (child_num == 'z')
{
continue;
}
}
delete num;
return;
}
int DeleteTree(TreeNode **root)
{
if (*root == NULL)
return 0;
if ((*root)->left != NULL)
DeleteTree(&((*root)->left));
if ((*root)->right != NULL)
DeleteTree(&((*root)->right));
delete *root;
return 0;
}
vector<int> PrintFromTopToBottom(TreeNode* root)
{
vector<int> res;
if (root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
res.push_back(q.front()->value); //将此时的queueTreeNode第一个元素存入到输出容器res中
if (q.front()->left != NULL) //若当前结点的左结点不为空,将其左结点存入到双端队列queueTreeNode中
q.push(q.front()->left);
if (q.front()->right != NULL) ////若当前结点的右结点不为空,将其右结点存入到双端队列queueTreeNode中
q.push(q.front()->right);
q.pop();
}
return res;
}
int main(void)
{
int m;
TreeNode *root = NULL;
while (cin >> m)
{
num = m;
if (m == 0)
{
cout << "NULL" << endl;
return 0;
}
CreateTree(&root, m);
PrintFromTopToBottom(root);
cout << endl;
DeleteTree(&root);
root = NULL;
}
return 0;
}