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:
97 8- -- -- -0 12 34 5- -- -
Sample Output 1:
YES 8
Sample Input 2:
8- -4 50 6- -2 3- 7- -- -
Sample Output 2:
NO 1
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 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 >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
注意点:完全二叉树的判断就是看已经有节点没孩子了,后面节点却还有孩子,这树就不是完全二叉树。有三个点一直错误,一开始段错误,后来在结构体初始化了-1后答案错误,总找不到原因。后来才发现原来是读输入的时候错了,两位数就读不到了,不能用%c,要用string