Description:

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 (≤), 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 1 and are separated by a space.

Then another positive integer M (≤) 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

Keys:

  • 二叉树的存储和遍历

Attention:

  • sscanf(s.c_str(), "%d %*s %d", &a, &b);
  • s.find("s") != string::npos;
  • map<int,node*> mp;

Code:

 /*
二叉树中各结点值为互不相同的正整数
给出后序遍历和中序遍历 判断所给语句是否正确
根节点
兄弟
父结点
左孩子
右孩子
同一层
满二叉树 基本思路:
建树,存储<键值,结点>的映射,存储结点所在层次,判断是否为满二叉树
sscanf读取字符串
*/
#include<cstdio>
#include<string>
#include<unordered_map>
#include<iostream>
using namespace std;
const int M=1e3+;
struct node
{
int data,high;
node *lchild,*rchild;
};
int in[M],pt[M],hs[M]={},fa[M]={},isfull=;
unordered_map<int,node*> mp; node *Create(int ptL, int ptR, int inL, int inR, int layer, int father)
{
if(ptL > ptR)
return NULL;
node *root = new node;
root->data = pt[ptR];
root->high = layer;
int k;
for(k=inL; k<=inR; k++)
if(in[k]==pt[ptR])
break;
int numLeft = k-inL;
root->lchild = Create(ptL,ptL+numLeft-,inL,k-,layer+, root->data);
root->rchild = Create(ptL+numLeft,ptR-,k+,inR,layer+, root->data);
hs[root->data]=;
fa[root->data]=father;
mp[root->data] = root;
if((root->lchild==NULL&&root->rchild!=NULL) || (root->lchild!=NULL&&root->rchild==NULL))
isfull=;
return root;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDEG int n,m;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &pt[i]);
for(int i=; i<n; i++)
scanf("%d", &in[i]); node *root=Create(,n-,,n-,,-);
scanf("%d\n", &m);
while(m--)
{
int v1,v2;
string st;
getline(cin,st);
if(st.find("root") != string::npos)
{
sscanf(st.c_str(), "%d is the root", &v1);
if(v1 == pt[n-])
printf("Yes\n");
else
printf("No\n");
}
else if(st.find("siblings") != string::npos)
{
sscanf(st.c_str(), "%d and %d are siblings", &v1,&v2);
if(hs[v1]== && hs[v2]== && fa[v1]==fa[v2])
printf("Yes\n");
else
printf("No\n");
}
else if(st.find("parent") != string::npos)
{
sscanf(st.c_str(), "%d is the parent of %d", &v1,&v2);
if(hs[v1]== && hs[v2]== && fa[v2]==v1)
printf("Yes\n");
else
printf("No\n");
}
else if(st.find("left") != string::npos)
{
sscanf(st.c_str(), "%d is the left child of %d", &v1,&v2);
if(hs[v1]== && hs[v2]== && mp[v2]->lchild==mp[v1])
printf("Yes\n");
else
printf("No\n");
}
else if(st.find("right") != string::npos)
{
sscanf(st.c_str(), "%d is the right child of %d", &v1,&v2);
if(hs[v1]== && hs[v2]== && mp[v2]->rchild==mp[v1])
printf("Yes\n");
else
printf("No\n");
}
else if(st.find("same") != string::npos)
{
sscanf(st.c_str(), "%d and %d are on the same level", &v1,&v2);
if(hs[v1]== && hs[v2]== && mp[v1]->high==mp[v2]->high)
printf("Yes\n");
else
printf("No\n");
}
else if(st.find("full") != string::npos)
{
if(isfull)
printf("Yes\n");
else
printf("No\n");
}
} return ;
}
 

PAT 2019-3 7-4 Structure of a Binary Tree的更多相关文章

  1. 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)

    题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...

  2. PAT 2019 春

    算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了. 7-1 Sexy Primes 读懂题意就行. #include <cstdio> #include & ...

  3. 【PAT 甲级】1151 LCA in a Binary Tree (30 分)

    题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...

  4. PAT 2019 秋

    考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...

  5. Navigation - How to define the structure of the navigation tree via the NavigationItemAttribute

    In the meantime, you should use the Model Editor to create such a navigation structure. There are se ...

  6. PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca

    给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...

  7. [Data Structure] 红黑树( Red-Black Tree ) - 笔记

    1.  红黑树属性:根到叶子的路径中,最长路径不大于最短路径的两倍. 2. 红黑树是一个二叉搜索树,并且有 a. 每个节点除了有左.右.父节点的属性外,还有颜色属性,红色或者黑色. b. ( 根属性 ...

  8. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

  9. PAT (Advanced Level) 1102. Invert a Binary Tree (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

随机推荐

  1. Linux mysql 乱码

    http://www.pc6.com/infoview/Article_63586.html http://itindex.net/detail/41748-linux-mysql-5.5 http: ...

  2. [fw]拦截系统调用

    今天在ubuntu中玩了下“拦截系统调用”,记录下自己对整个实现的理解. 原理 在linux kernel中,系统调用都放在一个叫做“sys_call_table”的分配表里面,在进入一个系统调用的最 ...

  3. bzoj2396 神奇的矩阵(随机化)

    Time Limit: 5 Sec  Memory Limit: 512 MB 给出三个行数和列数均为N的矩阵A.B.C,判断A*B=C是否成立. 题目可能包含若干组数据.    对于每组数据,第一行 ...

  4. C. DZY Loves Sequences

    C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. Android应用程序开发中碰到的错误和获得的小经验

    1,Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE Description:这表示手机内存不足,对内存较小的手机经常会出现这样的问题,从 ...

  6. FY20-ASE 开课!

    自我介绍 我叫陈志锴,undergraduate,pre-phd,初级程序员(c++和c的区别只知道多了类和对象这种,python只会写大作业代码和用基础的neural network框架),曾经跟着 ...

  7. 靶场练习--sqli(3&4)

    第三关 先解决一下第二关遗留下来的问题,嘻嘻.看来数据库原理应当过一遍~ 1.首先判断是否有SQL注入,然后再看是数字型.字符型.发现这里是字符型. 2.order by 查询字段数,记得后面要加一个 ...

  8. spring boot 不连接数据库启动

    Consider the following:    If you want an embedded database (H2, HSQL or Derby), please put it on th ...

  9. Django组件——用户认证

    用户认证 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1 .authentica ...

  10. pandas-同时处理两行数据

    pandas-同时处理两行数据 假设数据集data如下所示: 如果我们想要将user_id 和 item_id两列进行对应元素相加的操作,该怎么办呢? 显然我们先定义一个加法函数,然后使用apply函 ...