题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~

这道题的麻烦之处在于如何从一个字符串中去找数字。先首先我们需要整行读取(包括空格),这个我自己写另一个函数,挺好用的:

string mygets()
{char str[100];fgets(str,sizeof(str),stdin);int len = strlen(str);if(str[len-1]=='\n') str[len-1] = '\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")!=-1){int a = getNum(str,0);int b = getNum(str,str.find("of")+3);}

  以下是我的代码:

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 = 0;}
};
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+1;}else{node->right= Insert(node->right,val);node->right->level = node->level+1;}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")!=-1){int a = getNum(str,0);return a == root->val;}else if(str.find("siblings")!=-1){int a = getNum(str,0);int b = getNum(str,str.find("of")+3);//兄弟节点,必须在同一层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")!=-1){int a = getNum(str,0);int b = getNum(str,str.find("of")+3);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")!=-1){int a = getNum(str,0);int b = getNum(str,str.find("of")+3);if(valtonode[b]&&valtonode[a]){if(valtonode[b]->left==valtonode[a])return true;}return false;}else if(str.find("right")!=-1){int a = getNum(str,0);int b = getNum(str,str.find("of")+3);if(valtonode[b]&&valtonode[a]){if(valtonode[b]->right==valtonode[a])return true;}}else if(str.find("same")!=-1){int a = getNum(str,0);int b = getNum(str,str.find("and")+4);if(valtonode[b]&&valtonode[a]){if(valtonode[b]->level==valtonode[a]->level)return true;}return false;}else if(str.find("full")!=-1){return isFullTree(root);}return false;
}
string mygets()
{char str[100];fgets(str,sizeof(str),stdin);int len = strlen(str);if(str[len-1]=='\n') str[len-1] = '\0';return str;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("1.txt","r",stdin);#endifscanf("%d",&n);int post[n];for(int i=0;i<n;++i)scanf("%d",&post[i]);for(int i=0;i<n;++i){int a;scanf("%d",&a);valtoid[a] = i;}for(int i=n-1;i>=0;--i)root = Insert(root,post[i]);int m;scanf("%d",&m);getchar();for(int i=0;i<m;++i){string str = mygets();bool bRight = isRight(str);printf("%s\n",bRight?"Yes":"No");}return 0;
}

View Code

题目在下面:

7-4 Structure of a Binary Tree (30 分)
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 (≤30), 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 10
​3
​​  and are separated by a space.Then another positive integer M (≤30) 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 treeSample Output:
Yes
No
Yes
No
Yes
Yes
Yes

 

转载于:https://www.cnblogs.com/jlyg/p/10470202.html

2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)相关推荐

  1. 【PAT】2020年春季考试乙级题目、答案、摸鱼、游记、93分

    T1 对称日 (15分) 7-1 对称日 (15分) 央视新闻发了一条微博,指出 2020 年有个罕见的"对称日",即 2020 年 2 月 2 日,按照 年年年年月月日日 格式组 ...

  2. PAT(甲级)2019年春季考试 7-4 Structure of a Binary Tree

    目录 整体思路 犯的错误 代码 整体思路 1.先根据后序和中序序列建树,老生常谈,记得返回root 2.对树进行BFS,在这个过程中用hash的方式记录下每个值对应的父节点.左孩子.右孩子的值,记录下 ...

  3. hbuilderx怎么添加断点_【高考语文题库】高考一直提分提不上去该怎么办?同一卷高考押题语文答案,助你再提30分...

    大树从来不是在温室里长成的,而是在风霜雪雨的洗礼中参天的--古语云:"工欲善其事,必先利其器."俗话说:"磨刀不误砍材工."用到学习上就是学习必须讲究学习方法, ...

  4. 2018春考计算机技能考试题目,2018年山东省春季高考技能考试信息技术类专业考试(样题).PDF...

    2018年山东省春季高考技能考试信息技术类专业考试(样题).PDF 2018 年山东省春季高考技能考试信息技术类专业考试 (样题 ) 考试说明 1 .考试时间为 60分钟 : 2 .考试内容包括图形图 ...

  5. 牛客网 vivo2020届春季校园招聘在线编程考试 第3题

    牛客网 vivo2020届春季校园招聘在线编程考试 第3题 1.问题分析 2.问题解决 3.总结 1.问题分析   主要就是两个数学公式.代码我已经进行了详细的注释,理解应该没有问题,读者可以作为参考 ...

  6. 牛客网 vivo2020届春季校园招聘在线编程考试 第2题

    牛客网 vivo2020届春季校园招聘在线编程考试 第2题 1.问题分析 2.问题解决 3.总结 1.问题分析 基本思路是: 1.如果有因子大于等于 10 ,说明不存在 m,使得 m 的各位(个位.十 ...

  7. 2022汽车驾驶员(高级)考试模拟100题及在线模拟考试

    题库来源:安全生产模拟考试一点通公众号小程序 2022汽车驾驶员(高级)操作证考试题库系汽车驾驶员(高级)上岗证题目考前押题密卷!2022汽车驾驶员(高级)考试模拟100题及在线模拟考试依据汽车驾驶员 ...

  8. 2022衢州江山农商银行春季招聘考前冲刺题及答案

    题库来源:优题宝公众号 2022衢州江山农商银行春季招聘考前冲刺题及答案,由优题宝公众号根据最新农村信用社大纲与历年真题汇总编写,包含农村信用社常考重点题型与知识点,有助于考生复习备考农村信用社,取得 ...

  9. 2014年高考计算机试题答案,春季高考历年真题-2014年天津市春季高考计算机试卷.doc...

    文档介绍: 春季高考历年真题-2014年天津市春季高考计算机试卷2014年天津市高等院校春季招生统一考试计算机基础本试卷分为第Ⅰ卷(选择题)和第Ⅱ卷(非选择题)两部分,共150分.考试用时90分钟.第 ...

最新文章

  1. Java Review - 并发编程_DelayQueue原理源码剖析
  2. linux脚本重命名文件,shell脚本批量对文件改名(名字新旧不相关)
  3. OpenCV实现Mat与vector,Mat与数组互转
  4. halcon file_exists 检查文件是否存在
  5. sql 注射_令人惊讶的注射
  6. CSS 中的内联元素、块级元素以及display的各个属性的特点
  7. c++访问数据库代码示例 occi_使用Python操作SQL Server数据库
  8. 电脑遇到问题需要重新启动_如何解决电脑风扇转一下就停开不了机的问题-系统城...
  9. leetcode [27] 移除元素 / Remove Element
  10. GUI版Hex合并和Hex转换工具-HexMergeTool 取代hex2bin命令行工具
  11. WEB安全-ESAPI
  12. 最简单播放m3u8链接的方法
  13. 初中计算机 课题研究,初中信息技术的教研课题题目
  14. 第六届CCF计算机职业资格认证考试题解(C++)
  15. 腾讯内部深度文章曝光:微信向左 手机QQ向右
  16. 普及练习场 深度优先搜索 八皇后
  17. 凌恩生物明星产品:一文读懂细胞器基因组!
  18. mysql skip 1062_【20180205】MySQL 1032和1062跳过错误总结
  19. 数据中心远程集中解决方案有哪些?
  20. 租用国外服务器应该注意哪些?

热门文章

  1. JavaScript实现图片自动轮播
  2. bbr linux内核,linux手动配置BBR
  3. 使用css3实现雪碧图帧动画
  4. .net5创建WebApi项目入门教程
  5. OCR扫描识别录入之安卓Android行驶证驾驶证识别SDK
  6. linux 设置显卡命令,Linux 命令行下如何配置nVIDIA显卡 之三
  7. 人脸识别——脸部属性辅助(得分层)
  8. PHP goto语句加密的解密全过程(实战)
  9. 项目整体管理:结束项目阶段--收尾过程组
  10. InnoDB的全文索引