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. Eclipse常见版本和JDK常用版本对应关系

    Luna 4.4  JDK1.6Mars 4.5  JDK1.7             Neon 4.6  JDK1.8Oxygen 4.7 JDK1.8Photon 4.8  2019年3月

  2. python学习第十一天列表的分片和运算

    列表的分片也叫切片,也就是从列表中取出一段赋值给另外一个变量,列表运算就是可以进行比较运算,连接运算,乘法运算等. 1,列表的分片 n1=[1,2,3,4,5,6,7,8,9] n2=[1:3] 包含 ...

  3. 基于mesos 安装 jenkins

    mesos master 机子上安装  jenkins git clone https://github.com/jenkinsci/mesos-plugin.git && cd me ...

  4. 使用wkhtmltopdf工具生成pdf

    背景:将前台页面转换成pdf文档保存到服务器 最开始计划使用canvas2pdf在前端进行生成.但是canva2pdf转换的pdf有严重的失真问题,然后决定使用wkhtmltopdf工具进行生成. 思 ...

  5. 【记录】elastiasearch 6.4.3 版本 SearchRequestBuilder字段排序,BoolQueryBuilder

    参考地址: 1:https://www.cnblogs.com/shoutn/p/8027960.html 2:https://www.jianshu.com/p/8402eb930ae7 3:htt ...

  6. 机器学习-线性回归(基于R语言)

    基本概念 利用线性的方法,模拟因变量与一个或多个自变量之间的关系.自变量是模型输入值,因变量是模型基于自变量的输出值. 因变量是自变量线性叠加和的结果. 线性回归模型背后的逻辑——最小二乘法计算线性系 ...

  7. GC(垃圾回收器)中的算法

    GC的两种判定方法 (1) 引用计数法 给对象添加一个引用计数器,每当引用一次+1,每次失效时-1,当计数器为0时,表示对象就是不可能再被使用的. (2) 可达性分析算法 将“GC Roots”对象作 ...

  8. Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...(转)

    对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...

  9. Hadoop的配置文件

    hadoop-env.sh:脚本中所用到的环境变量,以运行Hadoop mapred-env.sh:脚本中所用到的环境变量,以运行mapreduce yarn-env.sh:脚本中所用到的环境变量,以 ...

  10. python方法参数:*和**操作符

    * def test_args_kwargs(arg1, arg2, arg3): print("arg1:", arg1) print("arg2:", ar ...