题外话:考试的时候花了一个小时做了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. linux 【第五篇】特殊权限及定时任务

    特殊权限 [root@VM_141_154_centos ~]# ls -ld /tmp drwxrwxrwt. 8 root root 4096 Apr 5 08:11 /tmp /tmp/ 公共目 ...

  2. Hackrank Candies DP

    题目链接:传送门 题意: n个学生站一行,老师给每个学生发至少一个糖 相邻学生,a[i] > a[i-1] 的话,那么右边学生的糖一定要发得比左边学生的糖多 问你满足条件这个老师总共最少的发多少 ...

  3. Fri Jul 28 16:28:52 CST 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection mus

    Fri Jul 28 16:28:52 CST 2017 WARN: Establishing SSL connection without server’s identity verificatio ...

  4. go---weichart个人对Golang中并发理解

    个人觉得goroutine是Go并行设计的核心,goroutine是协程,但比线程占用更少.golang对并发的处理采用了协程的技术.golang的goroutine就是协程的实现. 十几个gorou ...

  5. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  6. div+css布局教程系列2

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 谈一谈以太坊虚拟机EVM的缺陷与不足

    首先,EVM的设计初衷是什么?它为什么被设计成目前我们看的样子呢?根据以太坊官方提供的设计原理说明,EVM的设计目标主要针对以下方面: 简单性(Simplicity) 确定性(Determinism) ...

  8. SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)

    Problem description. The Fibonacci numbers defined as f(n) = f(n-1) + f(n-2) where f0 = 0 and f1 = 1 ...

  9. Java-Runoob-高级教程-实例-字符串:09. Java 实例 - 字符串小写转大写

    ylbtech-Java-Runoob-高级教程-实例-字符串:09. Java 实例 - 字符串小写转大写 1.返回顶部 1. Java 实例 - 字符串小写转大写  Java 实例 以下实例使用了 ...

  10. php 和 js之间使用json通信

    有时候我们需要用后台从数据库中得到的数据在js中进行处理,但是当从php中获取到数据的时候,使用的是键值对形式的多维关联数组.而我们知道,js只支持索引数组,不支持关联数组,这个时候从后台传递过来的数 ...