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.
A 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 103 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
这道题难度不大,考察对数据的处理能力。
以下是对题目的分析要点
1、树中的结点的值互不相同
2、值为正整数,并且范围在103
3、给出的两个数一定存在于这棵树中
4、对字符串处理,可以读入一行,使用streamstring 进行流操作来切割字符串,每行字符串都有关键字,可以根据关键字,来确定是什么内容。
5、最后输出就行了,层次遍历,用数组depth,par,child把所有需要使用的信息存储起来。
#include <iostream>
#include <cstdio>
#include <queue>
#include <sstream>
#include <cstdlib>
using namespace std;
struct node{
int data;
int level;
node *lchild,*rchild;
};
int N,M;
int a[35],b[35];
int depth[1005]={0};
int par[1005]={0}; //记录每个结点所属的父亲结点
int child[1005][2]={0};//记录每个结点的孩子
bool full=true;
int index;
void creat_Tree(int l1,int r1,int l2,int r2,node* &curr)
{
if(l1>r1)
return ;
curr=new node;
curr->data=a[r1];
curr->lchild=NULL;
curr->rchild=NULL;
int i;
for(i=l2;i<=r2;i++)
if(b[i]==a[r1])
break;
creat_Tree(l1,l1+i-1-l2,l2,i-1,curr->lchild); //左子树
creat_Tree(l1+i-l2,r1-1,i+1,r2,curr->rchild); //右子树
}
void level_tra(node *root) //获得每一个结点的深度
{
queue<node*> que;
root->level=1;
que.push(root);
while(!que.empty())
{
node *curr=que.front();
depth[ curr->data ] = curr->level; //记录每个数的深度
que.pop();
if( !((curr->lchild&&curr->rchild) || (curr->lchild==NULL&&curr->rchild==NULL)) )
full=false;
//如果存在一个结点,不满足上述条件,则为非full
if(curr->lchild)
{
curr->lchild->level = curr->level + 1;
par[curr->lchild->data]=curr->data;
child[curr->data][0]=curr->lchild->data;
que.push(curr->lchild);
}
if(curr->rchild)
{
curr->rchild->level = curr->level + 1;
par[curr->rchild->data]=curr->data;
child[curr->data][1]=curr->rchild->data;
que.push(curr->rchild);
}
}
}
int main()
{
cin>>N;
for(int i=1;i<=N;i++)
scanf("%d",&a[i]);
for(int i=1;i<=N;i++)
scanf("%d",&b[i]);
node *root=NULL;
creat_Tree(1,N,1,N,root);
level_tra(root);
cin>>M;
int A,B;
getchar();
while(M--)
{
string ques;
getline(cin,ques);
stringstream ss(ques);
string str;
A=0;
B=0;
while(ss>>str)
{
if(str[0]>='0'&&str[0]<='9')
{
if(A==0)
A=atoi( str.c_str() );
else
B=atoi( str.c_str() );
}
if(str=="root")
index=0;
else if(str=="siblings")
index=1;
else if(str=="parent")
index=2;
else if(str=="left")
index=3;
else if(str=="right")
index=4;
else if(str=="same")
index=5;
else if(str=="full")
index=6;
}
switch(index){
case 0:
if(A==root->data)
printf("Yes\n");
else
printf("No\n");
break;
case 1:
if( par[A]==par[B] )
printf("Yes\n");
else
printf("No\n");
break;
case 2:
if(A==par[B])
printf("Yes\n");
else
printf("No\n");
break;
case 3:
if(child[B][0]==A)
printf("Yes\n");
else
printf("No\n");
break;
case 4:
if(child[B][1]==A)
printf("Yes\n");
else
printf("No\n");
break;
case 5:
if(depth[A]==depth[B])
printf("Yes\n");
else
printf("No\n");
break;
case 6:
if(full)
printf("Yes\n");
else
printf("No\n");
break;
}
}
}