PAT 2019-3 7-4 Structure of a Binary Tree
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.
- 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 (≤), 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
AandBin the statements are in the tree.
Output Specification:
For each statement, print in a line
Yesif it is correct, orNoif 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的更多相关文章
- 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)
题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...
- PAT 2019 春
算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了. 7-1 Sexy Primes 读懂题意就行. #include <cstdio> #include & ...
- 【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 ...
- PAT 2019 秋
考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...
- 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 ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- [Data Structure] 红黑树( Red-Black Tree ) - 笔记
1. 红黑树属性:根到叶子的路径中,最长路径不大于最短路径的两倍. 2. 红黑树是一个二叉搜索树,并且有 a. 每个节点除了有左.右.父节点的属性外,还有颜色属性,红色或者黑色. b. ( 根属性 ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
随机推荐
- 耗时近一个月,终于录完了VUE.JS2.0前端视频教程!
这次课录制的比较辛苦,圣诞节时原本已经快录制完成了,偶然的一次,播放了一下,感觉不满意,好几篇推倒重来,所以今天才结束. vue.js2.0是Vue.JS的最新版本,视频教程还不多,如果你看到了,学到 ...
- python常用的时间方法
from time import strftime setTime = strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) // In ...
- vue 中公共样式
html, body, h1, h2, h3, h4, h5, h6, p, textarea, input, select, ul, ol, li, fieldset, figure { margi ...
- .net 异步
原文:https://www.cnblogs.com/wisdomqq/archive/2012/03/26/2412349.html 在说到异步前,先来理一下几个容易混淆的概念,并行.多线程.异步. ...
- SQL 一次插入多次数据
数据插入 INSERT INTO 表名称 VALUES (值1, 值2,....) 指定所要插入数据的列 INSERT INTO table_name (列1, 列2,...) VALUES (值1, ...
- 【记录】eclipse jar包看不了源码
第一步:下载JAD . jad官方地址的官方下载地址是: http://www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasem ...
- vue,一路走来(17)--底部tabbar切换
<router-link></router-link>存在router-link-active属性,那么底部tabbar切换就简单多了.不会再出现刷新回到第一个的bug. &l ...
- python常用函数 M
max(iterable) 求最大值,可以传入key. 例子: min(iterable) 求最小值,支持传入key. 例子: match(regular expression, string) 字符 ...
- 手写9x9乘法表,冒泡排序
手写9x9乘法表,冒泡排序 9x9乘法表 class Demo {public static void main(String[] args) {for(int x = 0;x <= 9; x+ ...
- postgresql绿色版安装及Navicat创建数据库,导入导出sql
转载:https://www.cnblogs.com/winkey4986/p/5360551.html 1.设置安装路径为:D:\soft\pgsql,数据存储路径为:D:\soft\pgsql\d ...