A1110. Complete Binary Tree

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 N-1. 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<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<queue>
 6 using namespace std;
 7 typedef struct{
 8     int lchild, rchild;
 9 }node;
10 node tree[21];
11 int str2num(char ss[]){
12     int len = strlen(ss);
13     if(ss[0] == '-')
14         return -1;
15     int P = 1, ans = 0;
16     for(int i = len - 1; i >= 0; i--){
17         ans += (ss[i] - '0') * P;
18         P *= 10;
19     }
20     return ans;
21 }
22 int N, lastNode, root, notRoot[21] = {0}, cnt = 0, tag = 1;
23 void levelOrder(int root){
24     queue<int> Q;
25     Q.push(root);
26     while(Q.empty() == false){
27         int temp = Q.front();
28         lastNode = temp;
29         Q.pop();
30         cnt++;
31         if(cnt < N / 2 && (tree[temp].lchild == -1 || tree[temp].rchild == -1)){
32             tag = 0;
33             return;
34         }else if(cnt == N / 2 && (tree[temp].lchild == -1 && tree[temp].rchild == -1 || tree[temp].lchild == -1 && tree[temp].rchild != -1)){
35             tag = 0;
36             return;
37         }else if(cnt > N / 2 && (tree[temp].lchild != -1 || tree[temp].rchild != -1)){
38             tag = 0;
39             return;
40         }
41         if(tree[temp].lchild != -1)
42             Q.push(tree[temp].lchild);
43         if(tree[temp].rchild != -1)
44             Q.push(tree[temp].rchild);
45     }
46 }
47 int main(){
48     scanf("%d", &N);
49     char str[10];
50     for(int i = 0; i < N; i++){
51         scanf("%s", str);
52         tree[i].lchild = str2num(str);
53         scanf("%s", str);
54         tree[i].rchild = str2num(str);
55         if(tree[i].lchild != -1)
56             notRoot[tree[i].lchild] = 1;
57         if(tree[i].rchild != -1)
58             notRoot[tree[i].rchild] = 1;
59     }
60     for(int i = 0; i < N; i++){
61         if(notRoot[i] == 0){
62             root = i;
63             break;
64         }
65     }
66     levelOrder(root);
67     if(tag == 0)
68         printf("NO %d", root);
69     else printf("YES %d", lastNode);
70     cin >> N;
71     return 0;
72 }
View Code

总结:

1、题目要求判断二叉树是否是完全二叉树。我的办法是二叉树的层序遍历,访问一个节点就cnt++。访问前 N/2 - 1个节点时要求必须都有左右孩子。第N/2个节点要求必须是左右都非空或左非空右为空。N/2之后的节点要求左右子树都必须空。 

2、网上看到还有更简单的方法,就是在层序遍历的时候把为-1的空节点也都加入队列。当访问时,遇到-1节点则查看cnt,如果cnt<N 则说明不是完全二叉树。另外注意,最后一个非空节点不一定是N-1。因为虽然是完全二叉树,但它的层次遍历节点序号不是按照0、1、2、3的顺序。

原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8573370.html