二叉树的遍历

说明:代码使用codeblocks编译,C++实现,个人编写,如有错误,还望指正。

fengjingtu

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>


using namespace std;

struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int v):val(v),left(NULL),right(NULL){
}
};
/*
构建这样一棵二叉树:

1
/ \
2 3
/\ /\
4 5 6 7
/ \
8 9

*/
void BuildTree(TreeNode* &root)
{
root=new TreeNode(1);
root->left=new TreeNode(2);
root->right=new TreeNode(3);
root->left->left=new TreeNode(4);
root->left->right=new TreeNode(5);
root->right->left=new TreeNode(6);
root->right->right=new TreeNode(7);
root->left->left->left=new TreeNode(8);
root->left->right->right=new TreeNode(9);

return ;
}
void DestroyTree(TreeNode* &root)
{
if(root==NULL)
return ;
if(root->left)
DestroyTree(root->left);
if(root->right)
DestroyTree(root->right);
delete root;
return ;

}

void preRecursionPrint(TreeNode*root)
{
if(root==NULL)
return ;
cout<<root->val<<" ";
if(root->left)
preRecursionPrint(root->left);
if(root->right)
preRecursionPrint(root->right);
return ;
}
void midRecursionPrint(TreeNode*root)
{
if(root==NULL)
return ;
if(root->left)
midRecursionPrint(root->left);
cout<<root->val<<" ";
if(root->right)
midRecursionPrint(root->right);
return ;
}
void afterRecursionPrint(TreeNode*root)
{
if(root==NULL)
return ;
if(root->left)
afterRecursionPrint(root->left);
if(root->right)
afterRecursionPrint(root->right);
cout<<root->val<<" ";

return ;
}


void preNonRecursionPrint(TreeNode*root)
{
if(root==NULL)
return ;
TreeNode * cur=root;
stack<TreeNode*>s;
while(cur)
{
cout<<cur->val<<" ";
if(cur->right)
{
s.push(cur->right);
}
if(cur->left)
cur=cur->left;
else if(!s.empty())
{
cur=s.top();
s.pop();
}
else
return ;
}

return ;
}

void midNonRecursionPrint(TreeNode*root)
{
if(root==NULL)
return ;
TreeNode * cur=root;
stack<TreeNode*>s;
while(cur || (!s.empty()))
{
while(cur)
{
s.push(cur);
cur=cur->left;
}
if(!s.empty())
{
cur=s.top();
s.pop();
cout<<cur->val<<" ";
if(cur->right)
cur=cur->right;
else
cur=NULL;
}
else
return ;

}
return ;
}
void afterNonRecursionPrint(TreeNode*root)
{
if(root==NULL)
return ;
TreeNode * cur=root;
stack<TreeNode*>s1;
stack<TreeNode*>s2;

while(cur || (!s1.empty()))
{
while(cur)
{
s1.push(cur);
s2.push(cur);
cur=cur->right;
}
cur = s1.top();
s1.pop();
cur =cur->left;
}
while(!s2.empty())
{
cur=s2.top();
s2.pop();
cout<<cur->val<<" ";
}
return ;
}

void widePrint(TreeNode*root)
{
if(root==NULL)
return ;
queue<TreeNode*>q;
q.push(root);
TreeNode*cur;
while(!q.empty())
{
cur=q.front();
if(cur)
cout<<cur->val<<" ";
q.pop();
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
return ;

}
int main()
{
TreeNode *root;
//构建二叉树
BuildTree(root);

preRecursionPrint(root);
cout<<endl;
preNonRecursionPrint(root);
cout<<endl;
midRecursionPrint(root);
cout<<endl;
midNonRecursionPrint(root);
cout<<endl;
afterRecursionPrint(root);
cout<<endl;
afterNonRecursionPrint(root);
cout<<endl;
widePrint(root);
cout<<endl;

//销毁二叉树
DestroyTree(root);
return 0;
}
1
2
3
4
5
6
7
8
9
运行结果:

1 2 4 8 5 9 3 6 7
1 2 4 8 5 9 3 6 7
8 4 2 5 9 1 6 3 7
8 4 2 5 9 1 6 3 7
8 4 9 5 2 6 7 3 1
8 4 9 5 2 6 7 3 1
1 2 3 4 5 6 7 8 9