1159 Structure of a Binary Tree (30)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.

Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:

  • A is the root
  • A and B are siblings
  • A is the parent of B
  • A is the left child of B
  • A is the right child of B
  • A and B are on the same level
  • It is a full tree

Note:

  • Two nodes are on the same level, means that they have the same depth.
  • full binary tree is a tree in which every node other than the leaves has two children.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 10^3 and are separated by a space.

Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A and B in the statements are in the tree.

Output Specification:

For each statement, print in a line Yes if it is correct, or No if not.

Sample Input:

9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree

Sample Output:

Yes
No
Yes
No
Yes
Yes
Yes

题目大意:二叉树中的所有值都是不同的正整数,给出它的节点数和后序、中序遍历。再给出m条关于二叉树结构的语句,判断它们是否正确。语句是以下之一:A是根结点,A和B是兄弟,A是B的父结点,A是B的左孩子,A是B的右孩子,A和B在同一层,这是一棵完全树。(完全树指除了叶子结点外,每个结点都有两个孩子)。

分析:先根据中序和后序建树,每个节点除了节点值、左孩子、右孩子,还要记录它的父节点和深度信息。之后判断当前语句是否正确。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;

typedef struct node
{
    int val,level;
    struct node *left,*right,*fat;
}node;

node *gettree(int *post,int lp,int rp,int *in,int li,int ri,node *p,int lev)
{
    node *root=(node*)malloc(sizeof(node));
    root->fat=p,root->val=post[rp-1],root->left=root->right=NULL,root->level=lev;
    int index,temp=post[rp-1];
    for(index=li;index<ri;++index)
        if(in[index]==temp)break;

    if(index>li)root->left=gettree(post,lp,rp-(ri-index),in,li,index,root,lev+1);
    if(ri-index>1)root->right=gettree(post,lp+index-li,rp-1,in,index+1,ri,root,lev+1);

//    if(root->left!=NULL)
//    {
//        if(root->right!=NULL)db3(root->val,root->left->val,root->right->val);
//        else db2(root->val,root->left->val);
//    }
//    else if(root->right!=NULL)db2(root->val,root->right->val);
//    else db1(root->val);

    return root;
}

//void preorder(node *root)
//{
//    if(root==NULL)return;
//    printf("%d ",root->val);
//    preorder(root->left);
//    preorder(root->right);
//}
//
//void inorder(node *root)
//{
//    if(root==NULL)return;
//    inorder(root->left);
//    printf("%d ",root->val);
//    inorder(root->right);
//}
//
//void postorder(node *root)
//{
//    if(root==NULL)return;
//    postorder(root->left);
//    postorder(root->right);
//    printf("%d ",root->val);
//}

void Free(node *root)
{
    if(root->left!=NULL)Free(root->left);
    if(root->right!=NULL)Free(root->right);
    free(root);
    return;
}

void judge_root(node *root,char *s)
{
    int num=0;
    for(int i=0;s[i]!=' ';++i)
        num=num*10+s[i]-'0';
    printf("%s\n",num==root->val?"Yes":"No");
    return;
}

void judge_siblings(node *root,char *s)
{
    int a,b,l=0;a=b=0;
    for(l=0;s[l]!=' ';++l)
        a=a*10+s[l]-'0';
    l+=5;
    for(;s[l]!=' ';++l)
        b=b*10+s[l]-'0';
//    db2(a,b);
    node *p,*q;p=q=NULL;
    queue<node*>que;que.push(root);
    while(!que.empty())
    {
        node *r=que.front();que.pop();
        if(r->val==a)p=r;
        if(r->val==b)q=r;

        if(q!=NULL&&p!=NULL)break;

        if(r->left!=NULL)que.push(r->left);
        if(r->right!=NULL)que.push(r->right);
    }
//    db2(p->val,q->val);
    printf("%s\n",p->fat==q->fat?"Yes":"No");
    return;
}

void judge_parent(node *root,char *s)
{
    int a,b,l=0,r=strlen(s);a=b=0;
    for(l=0;s[l]!=' ';++l)
        a=a*10+s[l]-'0';
    for(;s[r]!=' ';--r);r++;
    for(;s[r];++r)
        b=b*10+s[r]-'0';
//    db2(a,b);
    node *p,*q;p=q=NULL;
    queue<node*>que;que.push(root);
    while(!que.empty())
    {
        node *r=que.front();que.pop();
        if(r->val==a)p=r;
        if(r->val==b)q=r;

        if(q!=NULL&&p!=NULL)break;

        if(r->left!=NULL)que.push(r->left);
        if(r->right!=NULL)que.push(r->right);
    }
//    db2(p->val,q->val);
    printf("%s\n",p==q->fat?"Yes":"No");
    return;
}

void judge_left(node *root,char *s)
{
    int a,b,l=0,r=strlen(s);a=b=0;
    for(l=0;s[l]!=' ';++l)
        a=a*10+s[l]-'0';
    for(;s[r]!=' ';--r);r++;
    for(;s[r];++r)
        b=b*10+s[r]-'0';
//    db2(a,b);
    node *p,*q;p=q=NULL;
    queue<node*>que;que.push(root);
    while(!que.empty())
    {
        node *r=que.front();que.pop();
        if(r->val==a)p=r;
        if(r->val==b)q=r;

        if(q!=NULL&&p!=NULL)break;

        if(r->left!=NULL)que.push(r->left);
        if(r->right!=NULL)que.push(r->right);
    }
//    db2(p->val,q->val);
    printf("%s\n",p==q->left?"Yes":"No");
    return;
}

void judge_right(node *root,char *s)
{
    int a,b,l=0,r=strlen(s);a=b=0;
    for(l=0;s[l]!=' ';++l)
        a=a*10+s[l]-'0';
    for(;s[r]!=' ';--r);r++;
    for(;s[r];++r)
        b=b*10+s[r]-'0';
//    db2(a,b);
    node *p,*q;p=q=NULL;
    queue<node*>que;que.push(root);
    while(!que.empty())
    {
        node *r=que.front();que.pop();
        if(r->val==a)p=r;
        if(r->val==b)q=r;

        if(q!=NULL&&p!=NULL)break;

        if(r->left!=NULL)que.push(r->left);
        if(r->right!=NULL)que.push(r->right);
    }
//    db2(p->val,q->val);
    printf("%s\n",p==q->right?"Yes":"No");
    return;
}

void judge_level(node *root,char *s)
{
    int a,b,l=0;a=b=0;
    for(l=0;s[l]!=' ';++l)
        a=a*10+s[l]-'0';
    l+=5;
    for(;s[l]!=' ';++l)
        b=b*10+s[l]-'0';
//    db2(a,b);
    node *p,*q;p=q=NULL;
    queue<node*>que;que.push(root);
    while(!que.empty())
    {
        node *r=que.front();que.pop();
        if(r->val==a)p=r;
        if(r->val==b)q=r;

        if(q!=NULL&&p!=NULL)break;

        if(r->left!=NULL)que.push(r->left);
        if(r->right!=NULL)que.push(r->right);
    }
//    db2(p->val,q->val);
    printf("%s\n",p->level==q->level?"Yes":"No");
    return;
}

void judge_tree(node *root,char *s)
{
    queue<node*>que;que.push(root);
    while(!que.empty())
    {
        node *r=que.front();que.pop();
        if((r->left==NULL&&r->right!=NULL)||(r->left!=NULL&&r->right==NULL))
        {
            printf("No\n");
            return;
        }
        if(r->left!=NULL)que.push(r->left);
        if(r->right!=NULL)que.push(r->right);
    }
    printf("Yes\n");
    return;
}

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n;scanf("%d",&n);
    int post[n+5]={0},in[n+5]={0};
    for(int i=0;i<n;++i)scanf("%d",&post[i]);
    for(int i=0;i<n;++i)scanf("%d",&in[i]);
    node *p=gettree(post,0,n,in,0,n,NULL,0);

//    preorder(p);printf("reorder\n");
//    inorder(p);printf("inorder\n");
//    postorder(p);printf("postorder\n");

    int m;scanf("%d\n",&m);
    while(m--)
    {
        char s[100];fgets(s,100,stdin);
        int l=strlen(s);s[l-1]='\0',l--;
//        db2(s,s[l-1]);
        if(s[l-1]=='t')judge_root(p,s);
        else if(s[l-1]=='s')judge_siblings(p,s);
        else if(s[l-1]=='l')judge_level(p,s);
        else if(s[l-1]=='e')judge_tree(p,s);
        else
        {
            int index=0,f=0;
            for(index=0;index<l;++index)
            {
                if(s[index]==' ')f++;
                if(f==3)break;
            }
            if(s[index+1]=='l')judge_left(p,s);
            else if(s[index+1]=='r')judge_right(p,s);
            else judge_parent(p,s);
        }
    }
    Free(p);

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值