-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path树的层序输出.cpp
60 lines (51 loc) · 971 Bytes
/
树的层序输出.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
#include <iostream>
#include<cstdlib>
#include<vector>
using namespace std;
typedef struct BiTNode{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
BiTree CreateBiTree(){
char ch;
BiTree T;
cin>>ch;
if (ch == '#')
T = NULL;
else
{
T = new(BiTNode);
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;
}
void print(BiTree root)
{
if(root == NULL)
return;
vector<BiTree> vec;
vec.push_back(root);
int cur = 0;
int last = 1;
while(cur < vec.size()) {
last = vec.size();
while(cur < last) {
cout<<vec[cur]->data<<" ";
if(vec[cur]->lchild != NULL)
vec.push_back(vec[cur]->lchild);
if(vec[cur]->rchild != NULL)
vec.push_back(vec[cur]->rchild);
++cur;
}
cout<<endl;
}
}
int main(){
BiTree T;
T = CreateBiTree();
print(T);
system("PAUSE");
return 0;
}