题外话:考试的时候花了一个小时做了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 分)的更多相关文章

  1. LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)

    这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...

  2. 【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 ...

  3. 【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 ...

  4. 【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 ...

  5. 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 ...

  6. 【刷题-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 ...

  7. 【刷题-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 ...

  8. PAT(甲级)2017年春季考试

    PAT(甲级)2017年春季考试 A.Raffle for Weibo Followers #include<bits/stdc++.h> using namespace std; int ...

  9. [考试]NOIP2015模拟题2

    // 此博文为迁移而来,写于2015年7月22日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w72i.html 1.总 ...

随机推荐

  1. 解决CentOS java环境不生效的问题

    查看当前java版本 [root@localhost jdk1.6.0_45]# java -version openjdk version "1.8.0_65" OpenJDK ...

  2. npm, webpack, vue-cli, vue-x, axios

    1,什么是node.js,以及npm 简单的来说Node.js就是运行在服务端的JavaScript,是基于Chrome V8引擎的.npm是Node.js包的管理工具. 2,npm的安装和更新 No ...

  3. 常用的Atom插件

    1.simplified-chinese-menu 2.tree-view-finder 3.minimap 4.linter和linter-jshint 5.linter-js-standard 6 ...

  4. MARGIN-BEFORE MARGIN-AFTER MARGIN-START MARGIN-END

    总的来说:这是CSS3.0的对于文章段P容器的定义方法语句!display:block这个样式,只定义了P容器为一个块;后面四句是CSS3中的样式定义方法:-webkit-margin-before: ...

  5. Oracle用户、权限、角色管理学习(文字很系统)

     Oracle用户.权限.角色管理 2009-03-16 13:20:50 标签:oracle 数据库  休闲 职场 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明 ...

  6. 唯一性校验 impl 和 action

    IMPL方法实现 //  这个方法是通过ID修改数据 如果得到结果大于0 表明结果有重复 如果得到结果小于0 表明结果正确 @Override public boolean checkVersion( ...

  7. 数据库sqlite3的使用-Navicat的安装

    一:Navicat Navicat是一款著名的数据库管理软件,支持大部分主流数据库(包括SQLite) 1.Navicat的安装 (1)下载该软件后,先打开该软件 (2)把文件拖入到应用程序拷贝 (3 ...

  8. BZOJ_4199_[Noi2015]品酒大会_后缀自动机

    BZOJ_4199_[Noi2015]品酒大会_后缀自动机 Description 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 酒家”和“首席 ...

  9. 循环冗余检验 (CRC) 算法原理

    Cyclic Redundancy Check循环冗余检验,是基于数据计算一组效验码,用于核对数据传输过程中是否被更改或传输错误. 算法原理 假设数据传输过程中需要发送15位的二进制信息g=10100 ...

  10. appium学习【五】【转】appium实现屏幕向左滑动

    转自http://www.cnblogs.com/testhub/p/5949668.html 前些日子写一个滑动手机页面的小脚本,看到大家给的内容都是swipe方法,这里对swipe方法做一个小介绍 ...