2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)
题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~
这道题的麻烦之处在于如何从一个字符串中去找数字。先首先我们需要整行读取(包括空格),这个我自己写另一个函数,挺好用的:
string mygets()
{
char str[];
fgets(str,sizeof(str),stdin);
int len = strlen(str);
if(str[len-]=='\n') str[len-] = '\0';
return str;
}
用的时候只要 string str = mygets();就可以了,里面已经去掉换行符了。
下一步是再写一个从istart位置读取数字的函数,先从istart位置读取字符串(遇到空格,读取结束),然后用atoi把字符串转换成数字。
//字符串中istart位置开始取某个单词(以空格结束)
int getNum(string str,int istart)
{
int len = str.length();
string strA="";
for(int i=istart;i<len;++i)
if(str[i]==' ') break;
else strA += str[i];
//printf("%s\n",strA.c_str());
int a = atoi(strA.c_str());
return a;
}
题目中的字符串格式太多了是吧,眼花缭乱:
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
拿字符串siblings举例吧,想用find函数
siblings字符串,如果返回不等于-1,表示就是此种类型,然后用以下代码获取数字8和2。注意find函数的用法,返回的是查找字符串的首地址,所以find "of"返回的是o出现的首地址,没有找到of则返回-1,否则返回o的首地址。然后我们可以想象一下istart+1是f,istart+2是空格,istart+3就是我们所要的数字出现的首地址。
if(str.find("siblings")!=-)
{
int a = getNum(str,);
int b = getNum(str,str.find("of")+);
}
以下是我的代码:
PS:如果我的构树代码看不懂,可以看我写的这篇文章让你懂得已知先序(后序)与中序遍历如何快速构树:https://www.cnblogs.com/jlyg/p/10402919.html
树的结构体中我加了level变量,记录当前节点的水平深度。其他看代码吧~~
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node{
int val;
int level;
Node* left;
Node* right;
Node(){
left = right = NULL;
level = ;
}
};
map<int,int> valtoid; //通过值去找id
map<int,Node*> valtonode; //通过值去找节点
//构造数
Node* Insert(Node* node,int val)
{
if(node==NULL)
{
node = new Node();
node->val= val;
valtonode[val] = node;
}
else if(valtoid[val] < valtoid[node->val])
{
node->left = Insert(node->left,val);
node->left->level = node->level+;
}
else
{
node->right= Insert(node->right,val);
node->right->level = node->level+;
}
return node;
}
Node* root = NULL;
int n;
//是不是满二叉树
bool isFullTree(Node* root)
{
queue<Node*> q;
q.push(root);
Node* node;
while(!q.empty())
{
node = q.front();
q.pop();
if(node->left)
q.push(node->left);
if(node->right)
q.push(node->right);
if(node->left&&!node->right || !node->left&&node->right)
return false;
}
return true;
}
//字符串中istart位置开始取某个单词(以空格结束)
int getNum(string str,int istart)
{
int len = str.length();
string strA="";
for(int i=istart;i<len;++i)
if(str[i]==' ') break;
else strA += str[i];
//printf("%s\n",strA.c_str());
int a = atoi(strA.c_str());
return a;
}
//是不是兄弟节点
bool isSiblings(Node* node,Node* child1,Node* child2)
{
if(node==NULL) return false;
if(node->left==child1&&node->right!=child2||
node->left==child2&&node->right!=child1) return false;
else if(node->left==child1&&node->right==child2||
node->left==child2&&node->right==child1) return true;
return isSiblings(node->left,child1,child2)||isSiblings(node->right,child1,child2);
}
//判断这一个这句话是不是对的
bool isRight(string str)
{ if(str.find("root")!=-)
{
int a = getNum(str,);
return a == root->val;
}
else if(str.find("siblings")!=-)
{
int a = getNum(str,);
int b = getNum(str,str.find("of")+);
//兄弟节点,必须在同一层
if(valtonode[b]&&valtonode[a]&&valtonode[b]->level==valtonode[a]->level)
{
if(isSiblings(root,valtonode[a],valtonode[b]))
return true;
}
return false;
}
else if(str.find("parent")!=-)
{
int a = getNum(str,);
int b = getNum(str,str.find("of")+);
if(valtonode[b]&&valtonode[a])
{
if(valtonode[a]->left == valtonode[b] || valtonode[a]->right == valtonode[b])
return true;
}
return false;
}
else if(str.find("left")!=-)
{
int a = getNum(str,);
int b = getNum(str,str.find("of")+);
if(valtonode[b]&&valtonode[a])
{
if(valtonode[b]->left==valtonode[a])
return true;
}
return false;
}
else if(str.find("right")!=-)
{
int a = getNum(str,);
int b = getNum(str,str.find("of")+);
if(valtonode[b]&&valtonode[a])
{
if(valtonode[b]->right==valtonode[a])
return true;
}
}
else if(str.find("same")!=-)
{
int a = getNum(str,);
int b = getNum(str,str.find("and")+);
if(valtonode[b]&&valtonode[a])
{
if(valtonode[b]->level==valtonode[a]->level)
return true;
}
return false;
}
else if(str.find("full")!=-)
{
return isFullTree(root);
}
return false;
}
string mygets()
{
char str[];
fgets(str,sizeof(str),stdin);
int len = strlen(str);
if(str[len-]=='\n') str[len-] = '\0';
return str;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
scanf("%d",&n);
int post[n];
for(int i=;i<n;++i)
scanf("%d",&post[i]);
for(int i=;i<n;++i)
{
int a;
scanf("%d",&a);
valtoid[a] = i;
}
for(int i=n-;i>=;--i)
root = Insert(root,post[i]);
int m;
scanf("%d",&m);
getchar();
for(int i=;i<m;++i)
{
string str = mygets();
bool bRight = isRight(str);
printf("%s\n",bRight?"Yes":"No");
}
return ;
}
题目在下面:
- Structure of a Binary Tree ( 分)
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
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: is the root
and are siblings
is the parent of
is the left child of
is the right child of
and are on the same level
It is a full tree Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes
2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)的更多相关文章
- LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)
这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...
- 【leetnode刷题笔记】Maximum Depth of binary tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【leetcode刷题笔记】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【leetcode刷题笔记】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- 【刷题-PAT】A1119 Pre- and Post-order Traversals (30 分)
1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...
- 【刷题-PAT】A1095 Cars on Campus (30 分)
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- PAT(甲级)2017年春季考试
PAT(甲级)2017年春季考试 A.Raffle for Weibo Followers #include<bits/stdc++.h> using namespace std; int ...
- [考试]NOIP2015模拟题2
// 此博文为迁移而来,写于2015年7月22日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w72i.html 1.总 ...
随机推荐
- 邮件:事务失败。 服务器响应为:DT:SPM 163 smtp
几年前我做的一个项目,日发邮件最高峰时几十万.自以为对邮件发送方面已经有了一定认识,所以近期机缘巧合之下,又有项目需要发送邮件,不禁自信满满,暗自庆幸能不手到擒来乎? 不想老革命遇到新问题.我原先的邮 ...
- JavaScript的高大强
1,JavaScript的引入方式 1.1>Script标签内写代码 <Script> //这里写JS代码的地方 </Script> 1.2>引入额外的JS文件 & ...
- A - BBQ Easy
Score : 200 points Problem Statement Snuke is having a barbeque party. At the party, he will make N ...
- POJ3164 Command Network —— 最小树形图
题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS Memory Limit: 131072K ...
- HDU - 2063 过山车(最大匹配数)(模板)
1.男生女生一起坐过山车,每一排有两个座位,但是有个条件,就是每个女生必须找个男生做同伴一起(但是女生只愿意和某几个男生中的一个做同伴),求最多可以有多少对男女生组合坐上过山车. 2.二分图的最大匹配 ...
- ASP.NET项目开发实战<<一键创建解决方案>>
视频演示地址:http://www.youku.com/playlist_show/id_23192838.html 第一步:创建项目需要的数据库 打开辅助开发工具,如下图 从左侧菜单找到 项目数据库 ...
- <hr />标签的颜色和粗细设定
设置<hr />的颜色和粗细,不需要用到style,直接用标签的color和size属性: <hr color="red" size="0.5" ...
- java翻译lua+c+openssl签名项目
原来项目中用openresty nginx+lua实现server,lua调用c动态链接库,来使用openss做签名,并生成130字节(128签名+2位自定义字节)长度的文件. nginx: loca ...
- 利用jenv安装maven, tomcat,zookeeper等
jenv有关的网站: http://jenv.io https://github.com/gcuisinier/jenv 1. 执行jenv安装 $ curl -L -s get.jenv.io | ...
- bzoj1048(记忆化搜索)
1048: [HAOI2007]分割矩阵 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1218 Solved: 890[Submit][Statu ...