Judging complete binary tree (sequential storage) ZKNUOJ

Description

If the binary tree T and the full binary tree with the same height are numbered, if T and the node with the same number of the full binary tree are in the same position, then the binary tree T is called a complete binary tree. Now judge whether a tree is a complete binary tree based on the connection of the edges.

Input

The input is divided into two parts:
the first part: an integer T, representing the number of test groups.
The second part: there are T groups of data, and the first line of each group has 2 integers n (0 < n < 1024) and r (1 < =r<=n), indicating the number of nodes and the root of the tree, the next n-1 lines have 2 integers a,b (1 <= a, b <= n) in each line, indicating that a node and b node have one The edges are connected, if a is the root node of b, then b is the left child node of a, if b is the root node of a, then a is the right child node of b (the data is guaranteed to be a tree rather than a Forest)

Output

For each set of tests, if the corresponding binary tree is a complete binary tree, output yes, otherwise output no

Sample Input

2
5 1
1 2
3 1
4 2
2 5
5 1
1 2
3 1
4 2
3 5

Sample Output

yes
no

sequential storage

#include<stdio.h>
int chazhao(int x,int y,int n,int a[],int *p);
int panduan (int a [], int n);
void Output(int a[],int n);
intmain()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,g;
        scanf("%d%d",&n,&g);
        int a[10000]={0};//Initialization
        a[1]=g;//The root node enters the array
        int x,y;
        int p=0,max=1;//max represents the maximum length of the array
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            a[i];
            int flag=chazhao(x,y,max,a,&p);//Find the position in x, y that is the root pointer and bring back the root
            if(flag)//Return 1 means x is the root
            {
                a[p*2]=y;
                if(p*2>max)//Update the maximum length
                {
                    max=p*2;
                }
            }
            else
            {
                a[p*2+1]=x;
                if(p*2+1>max)//Update the maximum length
                {
                    max=p*2+1;
                }
            }
        }
        //Output(a,max);
        int o=panduan(a,max);//Determine whether from a[1] to a[max] is all 0
        if(o==0)
        printf("no\n");
        if(o==1)
        printf("yes\n");
         
    }
}
int chazhao(int x,int y,int n,int a[],int *p)
{
    for(int i=1;i<=n;i++)
    {
        if(a[i]==x)
        {
            *p=i;
            return 1;
        }
        if(a[i]==y)
        {
            *p=i;
            return 0;
        }
    }
}
int panduan (int a [], int n)
{
    for(int i=1;i<=n;i++)
    {
        if(a[i]==0)
        return 0;
    }
    return 1;
}
void Output(int a[],int n)
{
    for(int i=1;i<=n;i++)
    {
        printf("%d",a[i]);
    }
    printf("\n");
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325399288&siteId=291194637