PTA Advanced 1159 Structure of a Binary Tree C++
目录
题目
Input Specification:
Output Specification:
Sample Input:
Sample Output:
思路
代码
题目
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 103 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
思路
这题也不是难题,可以在1167 Cartesian Tree 或1020 Tree Traversals的基础上对dfs函数进行改进,即在dfs函数里面统计当前子树的根节点的信息,包括:层次(level)、双亲(parent)和左右孩子(left、right)
关键技术:
1. 如何获取statement中的数字 --- --- 用正则表达式做匹配
在1164 Good in C这题中也用到了正则表达式匹配字符串,封装好的函数代码如下:
#include <regex>// 根据传入的正则表达式分割字符串
// str-即将要进行分割的字符串;sep-正则表达式;result-存放分割后结果的容器
void splitStringByRegex(string str,string sep,vector<int> &result){regex pattern(sep);sregex_token_iterator end;// 循环结束的标志 // 有-1时表示得到i是除正则表达式外的部分,没有-1时表示得到i是和正则表达式匹配的部分 for(sregex_token_iterator i(str.begin(),str.end(),pattern);i!=end;++i){ result.push_back(stoi(*i));}
}
2. 如何判断statement是哪一类别 --- --- 用str1.find(str2)函数
// str1中找到了str2
if(str1.find(str2)!=string::npos){... ...
}else{// 没找到时... ...
}
代码
#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <map>using namespace std;struct Node{int level;int parent;int left;int right;
};vector<int> postOrder,inOrder;
map<int,Node> nodes;
int hasOneChildNum=0; // 根据传入的正则表达式分割字符串
// str-即将要进行分割的字符串;sep-正则表达式;result-存放分割后结果的容器
void splitStringByRegex(string str,string sep,vector<int> &result){regex pattern(sep);sregex_token_iterator end;// 循环结束的标志 // 有-1时表示得到i是除正则表达式外的部分,没有-1时表示得到i是和正则表达式匹配的部分 for(sregex_token_iterator i(str.begin(),str.end(),pattern);i!=end;++i){ result.push_back(stoi(*i));}
}void dfs(int postLeft,int postRight, int inLeft,int inRight, int currentLevel, int parent){if(postLeft>postRight) return;// 当前子树的root是后序遍历序列的最后一个元素 int root=postOrder[postRight];// 确定root的level和parent Node node; node.level=currentLevel;node.parent=parent; // 找到子树根在中序遍历序列中的位置int rootIndex=inLeft;for(;rootIndex<=inRight;rootIndex++)if(inOrder[rootIndex]==root) break;// 左子树后序遍历序列的index范围:postLeft ~ postLeft+rootIndex-inLeft-1int leftTreeLeft=postLeft;int leftTreeRight=postLeft+rootIndex-inLeft-1;// 右子树后序遍历序列的index范围:postLeft+rootIndex-inLeft ~ postRight-1int rightTreeLeft=postLeft+rootIndex-inLeft;int rightTreeRight=postRight-1;// 确定root的左右孩子if(leftTreeRight>=leftTreeLeft) node.left=postOrder[leftTreeRight];else node.left=-1;if(rightTreeRight>=rightTreeLeft) node.right=postOrder[rightTreeRight];else node.right=-1;// 统计只有一个孩子的结点的数量if(node.left!=-1&&node.right==-1||node.left==-1&&node.right!=-1) hasOneChildNum++;nodes[root]=node;dfs(leftTreeLeft,leftTreeRight, inLeft,rootIndex-1, currentLevel+1, root);// 该树的左子树相同操作 dfs(rightTreeLeft,rightTreeRight, rootIndex+1,inRight, currentLevel+1, root);// 该树的右子树相同操作
}int main(int argc, char** argv) {int n;cin>>n;postOrder.resize(n);inOrder.resize(n);for(int i=0;i<n;i++) cin>>postOrder[i];for(int i=0;i<n;i++) cin>>inOrder[i];// 根据后序和中序遍历次序健全结点信息 dfs(0,n-1, 0,n-1, 0, -1); int m;cin>>m;cin.get();for(int i=0;i<m;i++){string statement; getline(cin,statement);// 从statement中获取结点data vector<int> nodeData;splitStringByRegex(statement,"[0-9]+",nodeData); bool flag=false;// statement是判断某个结点是否是树的根时 if(statement.find("root")!=string::npos){if(nodes[nodeData[0]].parent==-1) flag=true;}else if(statement.find("level")!=string::npos){// statement是判断两个结点是否在同一层时if(nodes[nodeData[0]].level==nodes[nodeData[1]].level) flag=true;}else if(statement.find("parent")!=string::npos){// statement是判断结点0是否是结点1的双亲if(nodes[nodeData[1]].parent==nodeData[0]) flag=true;}else if(statement.find("left")!=string::npos){// statement是判断结点0是否是结点1的左孩子 if(nodes[nodeData[1]].left==nodeData[0]) flag=true;}else if(statement.find("right")!=string::npos){// statement是判断结点0是否是结点1的右孩子 if(nodes[nodeData[1]].right==nodeData[0]) flag=true;}else if(statement.find("siblings")!=string::npos){// statement是判断结点0和结点1是否是兄弟 int parent=nodes[nodeData[0]].parent;if(nodes[parent].left==nodeData[1]||nodes[parent].right==nodeData[1]) flag=true;}else if(statement.find("full")!=string::npos){// statement是判断这棵树是否是一颗full树 if(hasOneChildNum==0) flag=true;}if(flag) cout<<"Yes"<<endl;else cout<<"No"<<endl;}return 0;
}
相关文章:
PTA Advanced 1159 Structure of a Binary Tree C++
目录 题目 Input Specification: Output Specification: Sample Input: Sample Output: 思路 代码 题目 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 un…...
CDN绕过技术总汇
注 本文首发于合天网安实验室 首发链接:https://mp.weixin.qq.com/s/9oeUpFUZ_0FUu6YAhQGuAg 近日HVV培训以及面试,有人问了CDN该如何绕过找到目标真实IP,这向来是个老生常谈的问题,而且网上大多都有,但是有些不够全面…...
算法训练营DAY51|300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
本期是求子序列的新的一期,题目前两道有一些相似之处,思路差不多,第三道有一点难度,但并不意味着第一道没有难度,没有做过该类型题的选手,并不容易解出题解。 300. 最长递增子序列 - 力扣(Leet…...
mac:彻底解决-安装应用后提示:无法打开“XXX”,因为无法验证开发者的问题;无法验证此App不包含恶意软件
mac从浏览器或其他电脑接收了应用,但是打开报错 目录报错解决办法一次性方法永久解决方法验证恢复应用验证报错 截图如下: 错误信息 无法打开“XXX”,因为无法验证开发者的问题;无法验证此App不包含恶意软件 解决办法 一次性方…...
CPU 指标 user/idle/system 说明
从图中看出,一共有五个关于CPU的指标。分别如下: User User表示:CPU一共花了多少比例的时间运行在用户态空间或者说是用户进程(running user space processes)。典型的用户态空间程序有:Shells、数据库、web服务器…… Nice N…...
Thinkphp大型进销存ERP源码/ 进销存APP源码/小程序ERP系统/含VUE源码支持二次开发
框架:ThinkPHP5.0.24 uniapp 包含:服务端php全套开源源码,uniapp前端全套开源源码(可发布H5/android/iod/微信小程序/抖音小程序/支付宝/百度小程序) 注:这个是全开源,随便你怎么开,怎么来&a…...
hgame2023 WebMisc
文章目录Webweek1Classic Childhood GameBecome A MemberGuess Who I AmShow Me Your BeautyWeek2Git Leakagev2boardSearch CommodityDesignerweek3Login To Get My GiftPing To The HostGopher Shopweek4Shared DiaryTell MeMiscweek1Where am I神秘的海报week2Tetris Master…...
67. Python的绝对路径
67. Python的绝对路径 文章目录67. Python的绝对路径1. 准备工作2. 路径3. 绝对路径3.1 概念3.2 查看绝对路径的方法4. 课堂练习5. 用绝对路径读取txt文件6. 加\改写绝对路径6.1 转义字符知识回顾6.2 转义字符改写7. 总结1. 准备工作 对照下图,新建文件夹和txt文件…...
VHDL语言基础-组合逻辑电路-加法器
目录 加法器的设计: 半加器: 全加器: 加法器的模块化: 四位串行进位全加器的设计: 四位并行进位全加器: 串行进位与并行进位加法器性能比较: 8位加法器的实现: 加法器的设计&…...
内存检测工具Dr.Memory在Windows上的使用
之前在https://blog.csdn.net/fengbingchun/article/details/51626705 中介绍过Dr.Memory,那时在Windows上还不支持x64,最新的版本对x64已有了支持,这里再总结下。 Dr.Memory源码地址https://github.com/DynamoRIO/drmemory,最新发…...
J6412四网口迷你主机折腾虚拟机教程
今天给大家做一个四网口迷你主机折腾虚拟机的安装教程,主机采用的是maxtang大唐NUC J6412 intel i226V四网口的迷你主机,这款主机它是不能直接装上NAS的,必须使用虚拟机系统,近期研究了下然后做了一个教程分享给大家。 首先需要做…...
电子招标采购系统—企业战略布局下的采购寻源
智慧寻源 多策略、多场景寻源,多种看板让寻源过程全程可监控,根据不同采购场景,采取不同寻源策略, 实现采购寻源线上化管控;同时支持公域和私域寻源。 询价比价 全程线上询比价,信息公开透明ÿ…...
elasticsearch 之 mapping 映射
当我们往 es 中插入数据时,若索引不存在则会自动创建,mapping 使用默认的;但是有时默认的映射关系不能满足我们的要求,我们可以自定义 mapping 映射关系。 mapping 即索引结构,可以看做是数据库中的表结构,…...
2023年rabbitMq面试题汇总2(5道)
一、如何确保消息接收⽅消费了消息?接收⽅消息确认机制:消费者接收每⼀条消息后都必须进⾏确认(消息接收和消息确认是两个不同操作)。只有消费者确认了消息,RabbitMQ才能安全地把消息从队列中删除。这⾥并没有⽤到超时…...
电视剧《狂飙》数据分析,正片有效播放市场占有率达65.7%
哈喽大家好,春节已经过去了,朋友们也都陆陆续续开工了,小编在这里祝大家开工大吉!春节期间,一大批电视剧和网剧上映播出,其中电视剧《狂飙》以不可阻挡之势成功成为“开年剧王”。这里小编整理了一些《狂飙…...
cas单点登录后重定向次数过多问题以及调试cas-dot-net-client
问题描述: web项目应用cas作为单点登录站点,登录后无法打开WEB项目的页面,报错,说重定向次数过多。 老实说,这种问题,以前遇到过不少,是我这种半桶水程序员的噩梦。解决这种问题,不…...
【监控】Prometheus(普罗米修斯)监控概述
文章目录一、监控系统概论二、基础资源监控2.1、网络监控2.2、存储监控2.3、服务器监控2.4、中间件监控2.5、应用程序监控(APM)三、Prometheus 简介3.1、什么是 Prometheus3.2、优点3.3、组件3.4、架构3.5、适用于什么场景3.6、不适合什么场景四、数据模…...
opencv+python物体检测【03-模仿学习】
仿照练习:原文链接 步骤一:准备图片 正样本集:正样本集为包含“识别物体”的灰度图,一般大于等于2000张,尺寸不能太大,尺寸太大会导致训练时间过长。 负样本集:负样本集为不含“识别物体”的…...
计算机科学基础知识第二节讲义
课程链接 运行环境:WSL Ubuntu OMZ终端 PS:看到老师终端具有高亮和自动补全功能,我连夜肝出oh-my-zsh安装教程,实现了此功能。 这节课主要讲变量的语法、控制流程、shell功能等内容。 修改终端用户名,输入密码后重启…...
openssl genrsa 命令详解
文章目录一、openssl genrsa 命令介绍二、openssl genrsa 命令的语法及选项三、实例1、生成512位的 RSA 秘钥,输出到屏幕。2、生成512位 RSA 私钥,输出到指定的文件 genrsa.txt3、生成 1024 位 RSA 秘钥,采用 des 算法加密,加密密…...
【kafka】Golang实现分布式Masscan任务调度系统
要求: 输出两个程序,一个命令行程序(命令行参数用flag)和一个服务端程序。 命令行程序支持通过命令行参数配置下发IP或IP段、端口、扫描带宽,然后将消息推送到kafka里面。 服务端程序: 从kafka消费者接收…...
黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门 
一、Solidity合约开发 下面是 Solidity 合约开发 的概念、代码示例及讲解,适合用作学习或写简历项目背景说明。 🧠 一、概念简介:Solidity 合约开发 Solidity 是一种专门为 以太坊(Ethereum)平台编写智能合约的高级编…...
Spring AI 入门:Java 开发者的生成式 AI 实践之路
一、Spring AI 简介 在人工智能技术快速迭代的今天,Spring AI 作为 Spring 生态系统的新生力量,正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务(如 OpenAI、Anthropic)的无缝对接&…...
【Java_EE】Spring MVC
目录 Spring Web MVC 编辑注解 RestController RequestMapping RequestParam RequestParam RequestBody PathVariable RequestPart 参数传递 注意事项 编辑参数重命名 RequestParam 编辑编辑传递集合 RequestParam 传递JSON数据 编辑RequestBody …...
Unit 1 深度强化学习简介
Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库,例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体,比如 SnowballFight、Huggy the Do…...
《C++ 模板》
目录 函数模板 类模板 非类型模板参数 模板特化 函数模板特化 类模板的特化 模板,就像一个模具,里面可以将不同类型的材料做成一个形状,其分为函数模板和类模板。 函数模板 函数模板可以简化函数重载的代码。格式:templa…...
推荐 github 项目:GeminiImageApp(图片生成方向,可以做一定的素材)
推荐 github 项目:GeminiImageApp(图片生成方向,可以做一定的素材) 这个项目能干嘛? 使用 gemini 2.0 的 api 和 google 其他的 api 来做衍生处理 简化和优化了文生图和图生图的行为(我的最主要) 并且有一些目标检测和切割(我用不到) 视频和 imagefx 因为没 a…...
