-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1102.cpp
88 lines (83 loc) · 1.64 KB
/
1102.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
//
// 1102.cpp
// 算法
//
// Created by 王怡凡 on 2017/9/6.
// Copyright © 2017年 王怡凡. All rights reserved.
//
#include <stdio.h>
#include<queue>
using namespace std;
const int maxn = 20;
int n,root,num=0;
struct Node {
int left,right;
}node[maxn];
int notroot[maxn];
void bfs(int root) {
queue<int> q;
q.push(root);
int num = 0;
while(!q.empty()) {
int now = q.front();
printf("%d",now);
num++;
if(num!=n) {
printf(" ");
} else {
printf("\n");
}
q.pop();
if(node[now].right!=-1) {
q.push(node[now].right);
}
if(node[now].left!=-1) {
q.push(node[now].left);
}
}
}
void inorder(int index) {
if(index==-1) {
return ;
}
inorder(node[index].right);
printf("%d",index);
num++;
if(num!=n) {
printf(" ");
} else {
printf("\n");
}
inorder(node[index].left);
}
int main() {
scanf("%d",&n);
int i;
char left, right;
for(i=0;i<n;i++) {
getchar();
scanf("%c %c",&left,&right);
if(left=='-') {
node[i].left = -1;
} else {
node[i].left = left - '0';
notroot[node[i].left] = 1;
}
if(right=='-') {
node[i].right = -1;
} else {
node[i].right = right - '0';
notroot[node[i].right] = 1;
}
// printf("%d %d\n",node[i].left,node[i].right);
}
for(i=0;i<n;i++) {
if(notroot[i]==0) {
root = i;
break;
}
}
bfs(root);
inorder(root);
return 0;
}