题外话:考试的时候花了一个小时做了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. Wiz笔记发布博客工具无法获取分类修复

    使用Wiz笔记可以很方便的将笔记发布到博客,而且支持markdwon书写,并且可以很方便的通过复制粘贴来插入图片. 用法:http://blog.wiz.cn/wiz-plugin-blog-writ ...

  2. 嵌入式驱动开发之---dm8127 中sensor 驱动的改变

    #IPNC_DEVICE := DM385IPNC_DEVICE := DM812x # Values are "LOW_POWER" and "FULL_FEATURE ...

  3. 关于mybatis的 insert into select 命令未结束问题

    关于mybatis的 insert into select 命令未结束问题,最后以为是sql写错了,可是,在plsql运行又没问题.最后还是解决这个问题. 是设置问题. ### Cause: java ...

  4. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...

  5. 基于FFMPEG SDK流媒体开发1---解码媒体文件流信息

    近期项目涉及到流媒体等开发,因为有过开发经验深知其难度所在,没办法仅仅能又一次拾起,最新版的SDK被改的一塌糊涂,只是大体的开发思路都是一样的,看多少书查多少资料都无用,一步一步的编写代码 才是学好的 ...

  6. 普通用户无法登陆SSH问题

    Linux正常情况下普通用户是可以登陆SSH的,除非系统管理员作了修改,如果没有修改的情况无法登陆可以尝试以下方法解决: 步骤/方法 1 查看 /etc/ssh/sshd_config文件 发现 ro ...

  7. Google Gson使用简介

    1.Google Gson在android studio的使用 gradle:compile 'com.google.code.gson:gson:2.2.4' 2.Gson 注解 @Expose 注 ...

  8. Get started with Sourcetree

    Understand the interface Bookmarks window From that window, select the Local or Remote buttons to vi ...

  9. YTU 2959: 代码填充--雨昕学矩阵

    2959: 代码填充--雨昕学矩阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 112  解决: 50 题目描述 雨昕开始学矩阵了.矩阵数乘规则:一个数k乘一个矩阵A还是一个矩阵,行数 ...

  10. yii2.0 ActiveRecord 查询汇总

    User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()->w ...