Given a tree, you are supposed to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (20) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.

Sample Input 1:

9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -

Sample Output 1:

YES 8

Sample Input 2:

8
- -
4 5
0 6
- -
2 3
- 7
- -
- -

Sample Output 2:

NO 1
 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <set>
 4 #include <string.h>
 5 #include <vector>
 6 #include <math.h>
 7 #include <queue>
 8 #include <iostream>
 9 #include <string>
10 using namespace std;
11 const int maxn = 30;
12 int n,k;
13 struct node{
14     int l=-1,r=-1;
15 }tree[maxn];
16 int vis[maxn]={0};
17 int main(){
18     scanf("%d",&n);
19     getchar();
20     for(int i=0;i<n;i++){
21         string c1,c2;
22         cin>>c1>>c2;
23         getchar();
24         int n1=-1,n2=-1;
25         if(c1!="-"){
26             n1=stoi(c1);
27             vis[n1]=1;
28         }
29         if(c2!="-"){
30             n2=stoi(c2);
31             vis[n2]=1;
32         }
33         tree[i].l=n1;
34         tree[i].r=n2;
35     }
36     int root=-1;
37     for(int i=0;i<n;i++){
38         if(vis[i]==0){
39             root=i;
40             break;
41         }
42     }
43     int flag=0,flag2=0,now;
44     queue<int> q;
45     q.push(root);
46     while(!q.empty()){
47         now=q.front();
48         q.pop();
49         //printf("%d ",now);
50         if(tree[now].l!=-1){
51             if(flag==0)q.push(tree[now].l);
52             else {
53                 flag2=1;
54                 break;
55             }
56         }
57         else{
58             flag=1;
59         }
60         if(tree[now].r!=-1){
61             if(flag==0)q.push(tree[now].r);
62             else {
63                 flag2=1;
64                 break;
65             }
66         }
67         else{
68             flag=1;
69         }
70         /*if(now!=-1){
71             flag++;
72             flag2=now;
73         }
74         else{
75             if(flag==n){
76                 printf("YES %d",flag2);
77             }
78             else{
79                 printf("NO %d",root);
80             }
81             return 0;
82         }
83         q.push(tree[now].l);
84         q.push(tree[now].r);
85         */
86     }
87     if(flag2==1)printf("NO %d",root);
88     else printf("YES %d",now);
89 }
View Code

注意点:完全二叉树的判断就是看已经有节点没孩子了,后面节点却还有孩子,这树就不是完全二叉树。有三个点一直错误,一开始段错误,后来在结构体初始化了-1后答案错误,总找不到原因。后来才发现原来是读输入的时候错了,两位数就读不到了,不能用%c,要用string