-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA1102
75 lines (69 loc) · 1.12 KB
/
A1102
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=110;
struct node{
int lchild,rchild;
}Node[maxn];
int n,num;
bool notRoot[maxn]={false};
int strToNum(char c){
if(c=='-'){
return -1;
}
else{
notRoot[c-'0']=true;
return c-'0';
}
}
int findRoot(){
for(int i=0;i<n;i++){
if(notRoot[i]==false){
return i;
}
}
}
void print(int id){
printf("%d",id);
num++;
if(num<n)printf(" ");
else printf("\n");
}
void BFS(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int now=q.front();
q.pop();
print(now);
if(Node[now].rchild!=-1)q.push(Node[now].rchild);
if(Node[now].lchild!=-1)q.push(Node[now].lchild);
}
}
void inOrder(int root){
if(root==-1){
return;
}
inOrder(Node[root].rchild);
print(root);
inOrder(Node[root].lchild);
}
int main(){
char lchild,rchild;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%*c%c %c",&lchild,&rchild);
Node[i].lchild=strToNum(lchild);
Node[i].rchild=strToNum(rchild);
}
int root=findRoot();
BFS(root);
num=0;
inOrder(root);
return 0;
}