PAT 甲级 1159 Structure of a Binary Tree

19年春季第四题 PAT甲级 1159 Structure of a Binary Tree(30分) 汇总贴 2020年3月PAT甲级满分必备刷题技巧 题目 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.... 阅读详情

1159 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 tree

Sample Output:

Yes
No
Yes
No
Yes
Yes
Yes

经验总结:

emmmm  以前考的所有的关于树的问题以及没有考过的树的问题的大汇总,当然也不算完全的汇总,刚开始没有想到sscanf的时候,真的是有种很绝望的感觉,因此,代码里的查询输入部分很不规整,因为现在没法提交,所以就先这么放出来吧= =。
首先,还是最熟悉的中序加后序建树,这个就很基础了,关键就在建完树之后,虽然这里的查询最多只有30条,我还是使用了一个新结构体,存储所有需要查询的问题的答案,如果每查询一次就进行一次搜索,我觉得可能会超时(当然也没有尝试到底会不会),关键就在BFS了,把parent,lchild,rchild,level,siblings,都存储进来了,这里说一下我为什么初始化为1010,因为他没说是正整数,所以我怕-1也会是数据,而题目又说了所有的数不会大于1000,所以设了个1010来表示空,这里还有个问题,就是siblings,很多人表示不认识,当然我也是猜的,就是兄弟的意思,之前隐约在哪里看过所以有一点点印象,这个如果不知道啥意思,确实就不好AC了。
至于输出,就像我刚开始说的,采用string加上sscanf,效果极佳,用过都说好!感谢sscanf让我看到了AC的曙光!!
这一题我的做法比较基础,当然也可以在利用中序加后序建树的同时,就将左右孩子以及父亲和层数的数据存储好,这样就不用后面进行复杂的BFS了,可以省下很多行代码。。。。
就这么多啦~ 其他问题请参考代码~~

AC代码

#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 35;
int n, m, in[maxn], post[maxn];
map<int, int> mp;
bool isfull = true;
struct node
{
	int data,level;
	node* lchild, *rchild;
};
struct save
{
	int lchild, rchild, siblings,level,parent;
	save() :lchild(1010), rchild(1010), siblings(1010), parent(1010){}
}save[maxn];
node * create(int inL, int inR, int postL, int postR)
{
	if (inL > inR)
		return NULL;
	int index;
	for (index = inL; index <= inR; ++index)
	{
		if (in[index] == post[postR])
			break;
	}
	node *root = new node();
	root->data = post[postR];
	root->lchild = create(inL,index-1,postL,postL+index-inL-1);
	root->rchild = create(index+1,inR,postL+index-inL,postR-1);
	return root;
}
void BFS(node *root)
{
	queue<node *> q;
	root->level = 1;
	q.push(root);
	while (q.size())
	{
		node *x = q.front();
		save[mp[x->data]].level = x->level;
		q.pop();
		if (x->lchild != NULL&&x->rchild == NULL || x->lchild == NULL&&x->rchild != NULL)
		{
			isfull = false;
		}
		if (x->lchild != NULL&&x->rchild != NULL)
		{
			save[mp[x->lchild->data]].siblings = x->rchild->data;
			save[mp[x->rchild->data]].siblings = x->lchild->data;
		}
		if (x->lchild != NULL)
		{
			save[mp[x->lchild->data]].parent = x->data;
			save[mp[x->lchild->data]].level = x->level + 1;
			save[mp[x->data]].lchild = x->lchild->data;
			x->lchild->level = x->level + 1;
			q.push(x->lchild);
		}
		if (x->rchild != NULL)
		{
			save[mp[x->rchild->data]].parent = x->data;
			save[mp[x->rchild->data]].level = x->level + 1;
			save[mp[x->data]].rchild = x->rchild->data;
			x->rchild->level = x->level + 1;
			q.push(x->rchild);
		}
	}
}
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", &post[i]);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &in[i]);
		mp[in[i]] = i;
	}
	node *root = create(0, n - 1, 0, n - 1);
	scanf("%d", &m);
	getchar();
	string temp;
	BFS(root);
	for (int i = 0; i < m; ++i)
	{
		getline(cin,temp);
		if (temp.find("full")!=string::npos)
		{
			printf("%s\n", isfull ? "Yes" : "No");
		}
		else if (temp.substr(temp.size() - 11, 11) == "is the root")
		{
			int x = stoi(temp.substr(0,temp.size()-12));
			printf("%s\n", x == post[n - 1] ? "Yes" : "No");
		}
		else if (temp.substr(temp.size() - 8, 8) == "siblings")
		{
			int a, b;
			sscanf(temp.c_str(),"%d and %d are siblings", &a, &b);
			printf("%s\n", save[mp[a]].siblings == b ? "Yes" : "No");
		}
		else if (temp.find("parent") != string::npos)
		{
			int a, b;
			sscanf(temp.c_str(),"%d is the parent of %d", &a, &b);
			printf("%s\n", save[mp[b]].parent == a ? "Yes" : "No");
		}
		else if (temp.find("left") != string::npos)
		{
			int a, b;
			sscanf(temp.c_str(), "%d is the left child of %d", &a, &b);
			printf("%s\n", save[mp[b]].lchild == a ? "Yes" : "No");
		}
		else if (temp.find("right") != string::npos)
		{
			int a, b;
			sscanf(temp.c_str(), "%d is the right child of %d", &a, &b);
			printf("%s\n", save[mp[b]].rchild == a ? "Yes" : "No");
		}
		else if (temp.find("same") != string::npos)
		{
			int a, b;
			sscanf(temp.c_str(), "%d and %d are on the same level", &a, &b);
			printf("%s\n", save[mp[a]].level == save[mp[b]].level ? "Yes" : "No");
		}
	}
	return 0;
}

 

CondaHTTPError: HTTP 404 NOT FOUND for url 用conda创建新环境时出现问题了。 阅读详情

相关推荐

DDS器件原理与应用(AD9833 AD9834)

是DDS的核心组成部分,用于实现相位的累加,并输出相应的幅值。相位累加器由M位宽加法器和M位宽寄存器组成,通过时钟控制,将上一次累加结果反馈到加法器输入端实现累加功能,从而使每个时钟周期内的相位递增数为,并取相位累加结果作为地址输出给ROM查找表部分。DDS的相位信息被存放在累加寄存器中,虽然幅值和相位不是线性关系,但寄存器累加值和相位可以是线性关系,很容易用寄存器的累加值表达相位信息。

m0_49968063的博客 6460

1159 Structure of a Binary Tree – PAT甲级真题

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

柳婼 の blog 956

3步搞定专利数据爬取:用Selenium绕过国家知识产权局反爬(附完整代码)

本文详细介绍了3种高效获取专利数据的技术方案,重点解析了使用Selenium绕过国家知识产权局反爬的实战方法,并提供了完整代码示例。文章还探讨了Requests+lxml轻量级方案和混合技术策略,帮助开发者根据需求选择最佳数据爬取方案,特别适合需要专利统计数据的开发者参考。

weixin_29029995的博客 1119

『1159』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 ...

weixin_52435933的博客 345

[原创]Linux 802.11n CSI tool安装教程(亲测可用)

Linux 802.11n CSI tool安装教程 前言 网上关于Intel5300 CSI tool的教程繁多,实际有效的可以说极少,各种疏漏错误。装的时候被坑无数次,终于调通。其实装这个工具并不太难,但是很多人把它弄得极其复杂,所以写这篇博客来帮助和我有一样困扰的人。我基本沿用的是官方文档旧版本的安装方案,针对其冗杂之处做了大量改进。 旧版安装教程: http://dhalperi....

u014645508的博客 4万+

手把手教你用FPGA驱动DAC8830:一个Verilog SPI驱动模块的完整实现与调试

本文详细介绍了如何使用FPGA通过Verilog实现DAC8830的SPI驱动模块,包括SPI通信机制解析、Verilog模块设计、代码优化及调试技巧。重点解决时钟同步、状态机设计等工程实践问题,适用于音频处理、仪器仪表等高精度数模转换场景。

weixin_26886855的博客 235

【PAT甲级】 1159 Structure of a Binary Tree

后面想到可以直接把求结点高度的操作可以直接揉到判断是否是full二叉树的操作里面,缩短了代码。(所以最后代码段主要分为:1、构建二叉树 2、判断是否是full二叉树,同时求出各结点高度 3、判断输入是何种类型,得出相应结果。新手刷题,遇到树知识点的大杂烩,一点一点把各个独立考点拼起来,纪念一下AC(代码太长,每个功能点分的太细了)

qq_44799453的博客 217

CanOpen PDO映射实战:从原理到配置的完整指南

本文详细解析了CanOpen PDO映射的原理与配置流程,从基础通信机制到实战配置步骤,涵盖禁用PDO、清空映射、逐项映射和使能PDO等关键环节。特别针对西门子G120变频器、台达伺服和倍福CX控制器等不同厂商设备,提供了具体的配置技巧和常见问题排查指南,帮助工程师快速掌握多厂商设备间的PDO通信配置。

weixin_30718391的博客 522

1159 Structure of a Binary Tree (30)

【代码】1159 Structure of a Binary Tree (30)

2401_88085478的博客 737

电源完整性分析:PDN阻抗与去耦电容设计——目标阻抗计算与电容选型深度实战

摘要:随着嵌入式处理器主频突破 GHz、多核架构普及,电源完整性(Power Integrity, PI)已成为高速 PCB 设计中不可忽视的关键环节。本文从 PDN(电源分配网络)阻抗模型出发,系统讲解目标阻抗计算方法、去耦电容选型策略、反谐振抑制技术,并结合 STM32F4 核心板的完整设计实例,提供一套可落地的电源完整性工程实践方案。电源完整性设计是一项系统工程,需要从目标阻抗计算电容选型反谐振抑制PCB 布局目标阻抗是设计的起点,由电源电压、纹波要求和瞬态电流决定去耦电容。

从事移动开发八年,csdn博客专家,阿里云特邀专家,华为云云享专家,高级网络信息安全工程师,大数据工程师(高级),python技术开发(高级),HarmonyOS 应用开发者高级认证,现在某学校任教。 1万+

重回童年的经典系列☀️| 【坦克大战3D版】游戏制作+解析

带你重回童年的经典系列——坦克大战3D版制作,感兴趣的可以看看呀~

努力前行,总会成为自己心中的那道光 5万+

PAT甲级 1159 Structure of a Binary Tree (30 分)

题意 根据中后序建树,然后对于不同的查询判断正确性 代码1 #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; typedef long long ll; int post[35]; int in[35]; struct tree{ int data; tree *left, *right, ...

darknight 731

ESP32-C3模组上跑通HTTPS分次传输文件(1)

ESP32-C3模组上跑通HTTPS分次传输文件(1)

phmatthaus的专栏 763

PAT甲级 1159 Structure of a Binary Tree

浙江大学PAT题目解答内容. Contribute to ZouJiu1/PAT development by creating an account on GitHub.GitHub - ZouJiu1/PAT: 浙江大学PAT题目解答内容。

邹九的个人博客 217

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

1159 Structure of a Binary Tree (PAT甲级)

开始用了一个很傻的办法,在建树的时候把所有正确的statement映射到1,最后判断的时候看读入的statement映射的值,是1就Yes, 0就No....可惜这样做最后一个测试点过不了,我估计是测试数据在前面加了0之类的,比如把"15 is the root"改成"015 is the root",这样我这个取巧的办法就通过不了……后来还是看柳婼的思路改了代码才AC的。

linh2006的博客 128

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

PAT甲级2019春季7-4 1159.Structure of a Binary Tree (30 分)

算法笔记总目录 关键英语单词解释 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 stateme

0 649

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

PAT甲级——1159 Structure of a Binary Tree (30 分)(这可能是二叉树大保健)

二叉树大保健

小楼一夜听春雨 991

1159. Structure of a Binary Tree (30)-PAT甲级真题 (构造树 & 哈希表)

给定一棵树的后序、中序遍历。然后询问m次,每次判断问句是否正确。询问格式:a是否是根,该树是否是满二叉树,a和b是否是兄弟、是否同层,a是否是b的左孩子、右孩子、双亲。

hzj913313180的博客 118

PAT甲级分类【树】

1147 Heaps (30分) 完全二叉树和堆的性质 知识点: 大顶堆:子节点比父节点小,根节点最大 postorder 后序遍历 levelorder 层序遍历 //1147 #include<bits/stdc++.h> using namespace std; int m,n; vector <int> v; void postorder(int index){ if(index>n)return; postorder(index*2); postorder(in

且行的小博客 447

PAT 1159 Structure of a Binary Tree

原题链接:PAT 1159 Structure of a Binary Tree(30分) 关键词:dfs建树 题目大意:给出二叉树的后序和中序序列,再给出几个陈述让你判断是否正确。

cwtnice的博客 797

PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【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 state...

weixin_42637495的博客 386

PAT A1159 Structure of a Binary Tree测试点3答案错误?

原题链接 测试点3 这道题,测试点3,真坑。但是我试出来为什么错误了。因为测试点3里面有一个陈述,关于谁是谁的父亲的陈述,这个陈述里面,两个数的值是相同的。类似于“A is the parent of A”,这时候,应该是错误,但是如果代码没有检测两个数是否相同,则可能出错。 注意 以及关于siblings的判断,也应该首先判断是否两个数相同。 只有当两个值不同、两个值不在同一层、两个值的父亲节点地址相同,siblings陈述才正确。 AC代码: #include <bits/stdc++.h>

weixin_44321570的博客 681

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

Structure of a Binary Tree (30分) 重复代码很多,可以修剪一下 #include<bits/stdc++.h> using namespace std; struct Node { int left,right; int level,parent; Node() { left=right=parent=-1; } } node[1005]; vector<int>in,post; void Level(int root) { queue&l

shshbdhsjjd的博客 356

2019.3 PAT甲级-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 state...

请赐教 442

1159 Structure of a Binary Tree(中序+后序建树|丑陋输入的处理|full tree的定义)

【代码】1159 Structure of a Binary Tree(中序+后序建树|丑陋输入的处理|full tree的定义)

qq_43543086的博客 182
上一篇: PAT 甲级 1158 Telefraud Detection
下一篇: PAT考试经验总结(甲乙级均适用)~~想满分的请看这里!~~
漫浸天空的雨色
博客等级 码龄15年 1303粉丝 523原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值