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-第二篇常用命令

    1.常用目录文件操作命令 cd:切换目录 格式:cd 目录 ls:显示文件和目录列表.可选参数: -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 ll:查看目录接口,相当于是ls ...

  2. [CF643E]Bear and Destroying Subtrees(期望,忽略误差)

    Description: ​ 给你一棵初始只有根为1的树 ​ 两种操作 ​ 1 x 表示加入一个新点以 x为父亲 ​ 2 x 表示以 x 为根的子树期望最深深度 ​ 每条边都有 \(\frac{1}{ ...

  3. MapReduce-WordCountDemo

    /** * @Author: dreamer Q * @Date: 2019/11/4 22:26 * @Version 1.0 * @Discription 使用MapReduce 开发 WordC ...

  4. Matomo(Piwik)安装说明-----------基于LNPM环境

    Matomo(Piwik)安装说明 安装前环境检查 Piwik要求PHP版本高于PHP5.5(选用PHP7.2) Piwik需要pdo和pdo_mysql或mysqli支持(选用mysqli) Piw ...

  5. K8S创建的相关yaml文件

    一.K8S-yaml的使用及命令 YAML配置文件管理对象 对象管理: # 创建deployment资源 kubectl create -f nginx-deployment.yaml # 查看dep ...

  6. 消费者与生产者---LinkedList方式模拟

    采用LinkedList数据结构方式来模拟消费者与生产者模型,小Demo import java.util.LinkedList; public class MyQueue { private fin ...

  7. SPOJ287 NETADMIN - Smart Network Administrator

    传送门[洛谷] 常见套路? 关键点连新建汇点 流量1 源点1 原图中的边 二分流量. 二分+判满流 做完了. 附代码. #include<cstdio> #include<cstri ...

  8. CentOS7 安装KVM

    检测 输入命令如果有输出表示CPU支持虚拟化 grep -E 'svm|vmx' /proc/cpuinfo 检查模块(保证有如下内容) lsmod | grep kvm 结果如下:(kvm_inte ...

  9. 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal

    题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...

  10. SQL Server 2008 R2 数据库备份文件.bak如何挂载到【阿里云&#183;独立虚拟主机数据库】上

    1.前言 8月份刚刚开始,公司[工作流]挂了,实体服务器会自动重启,数据库死活起不来,这可是计算工资提成的事儿,不能马虎! 在实体服务器中,找到了OA页面源码与数据库的.MDF 与 .LDF 等文件. ...