Pat a problem-1110. Complete binary tree (25)-(judging whether it is a full binary)

Source: Internet
Author: User

Test instructions: Determines whether a two-ary tree with a node n is a complete binary tree. Yes outputs the last node of the full binary tree, no output root node.

Achievements, and then respectively, the tree and the node tree is n two-tree comparison, statistics corresponding to the number of nodes, if n, is a complete binary tree, otherwise it is not.

#include <iostream>#include<cstdio>#include<algorithm>#include<string.h>using namespacestd;Const intmaxn= A;inttwo[6];intLastnode;structnode{intLeft,right; intID;} TREE[MAXN],CBT[MAXN];/*The input given by the two-fork tree and the node is a full binary tree of n is compared, calculate the corresponding number of nodes if the number of nodes is N, then the two-tree is a complete binary tree. I is the current node number of the complete binary tree. Root is the corresponding node of the input binary tree. */intCompareintRootintIintN) {    intres1=-1, res2=-1; if(root==-1){        return 0; }    if(i==N) lastnode=root;//record the ID of the last node of the full binary tree    intsum=0; if(2*i<=N) {Sum+=compare (tree[root].left,i*2, N); }    if(2*i+1<=N) {Sum+=compare (tree[root].right,i*2+1, N); }    BOOLflag1=false, flag2=false; if((2*i<=n&&tree[root].left!=-1)|| (2*i>n&&tree[root].left==-1)) Flag1=true; if((2*i+1<=n&&tree[root].right!=-1)|| (2*i+1>n&&tree[root].right==-1)) Flag2=true; if(Flag1 &&flag2) Sum++; returnsum;}intMain () {intN; scanf ("%d",&N); Charstr1[Ten],str2[Ten]; intA; intVIS[MAXN]; memset (Vis,-1,sizeof(VIS));  for(intI=0; i<n;i++) {scanf ("%s%s", STR1,STR2); Tree[i].id=i; if(str1[0]=='-') Tree[i].left=-1; Else{a=atoi (STR1); Tree[i].left=A; Vis[a]=1; }        if(str2[0]=='-') Tree[i].right=-1; Else{a=atoi (STR2); Tree[i].right=A; Vis[a]=1; }    }    intRoot;  for(intI=0; i<n;i++){        if(vis[i]==-1) Root=i; }    if(Compare (Root,1, n) = =N) {printf ("YES%d", Lastnode); }    Else{printf ("NO%d", Root); }    return 0;}
View Code

The method of solving the problem in this blog is better than mine, that is, traversing the two-fork tree by the level, each traversing a node cnt++, until 1. At this time cnt=n, yes, otherwise no.

http://www.liuchuo.net/archives/2158

Pat a problem-1110. Complete binary tree (25)-(judging whether it is a full binary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.