当前位置: 首页 > news >正文

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.
  • 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绕过技术总汇

注 本文首发于合天网安实验室 首发链接&#xff1a;https://mp.weixin.qq.com/s/9oeUpFUZ_0FUu6YAhQGuAg 近日HVV培训以及面试&#xff0c;有人问了CDN该如何绕过找到目标真实IP&#xff0c;这向来是个老生常谈的问题&#xff0c;而且网上大多都有&#xff0c;但是有些不够全面…...

算法训练营DAY51|300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组

本期是求子序列的新的一期&#xff0c;题目前两道有一些相似之处&#xff0c;思路差不多&#xff0c;第三道有一点难度&#xff0c;但并不意味着第一道没有难度&#xff0c;没有做过该类型题的选手&#xff0c;并不容易解出题解。 300. 最长递增子序列 - 力扣&#xff08;Leet…...

mac:彻底解决-安装应用后提示:无法打开“XXX”,因为无法验证开发者的问题;无法验证此App不包含恶意软件

mac从浏览器或其他电脑接收了应用&#xff0c;但是打开报错 目录报错解决办法一次性方法永久解决方法验证恢复应用验证报错 截图如下&#xff1a; 错误信息 无法打开“XXX”&#xff0c;因为无法验证开发者的问题&#xff1b;无法验证此App不包含恶意软件 解决办法 一次性方…...

CPU 指标 user/idle/system 说明

从图中看出&#xff0c;一共有五个关于CPU的指标。分别如下&#xff1a; User User表示&#xff1a;CPU一共花了多少比例的时间运行在用户态空间或者说是用户进程(running user space processes)。典型的用户态空间程序有&#xff1a;Shells、数据库、web服务器…… Nice N…...

Thinkphp大型进销存ERP源码/ 进销存APP源码/小程序ERP系统/含VUE源码支持二次开发

框架&#xff1a;ThinkPHP5.0.24 uniapp 包含:服务端php全套开源源码&#xff0c;uniapp前端全套开源源码&#xff08;可发布H5/android/iod/微信小程序/抖音小程序/支付宝/百度小程序&#xff09; 注&#xff1a;这个是全开源&#xff0c;随便你怎么开&#xff0c;怎么来&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. 准备工作 对照下图&#xff0c;新建文件夹和txt文件…...

VHDL语言基础-组合逻辑电路-加法器

目录 加法器的设计&#xff1a; 半加器&#xff1a; 全加器&#xff1a; 加法器的模块化&#xff1a; 四位串行进位全加器的设计&#xff1a; 四位并行进位全加器&#xff1a; 串行进位与并行进位加法器性能比较&#xff1a; 8位加法器的实现&#xff1a; 加法器的设计&…...

内存检测工具Dr.Memory在Windows上的使用

之前在https://blog.csdn.net/fengbingchun/article/details/51626705 中介绍过Dr.Memory&#xff0c;那时在Windows上还不支持x64&#xff0c;最新的版本对x64已有了支持&#xff0c;这里再总结下。 Dr.Memory源码地址https://github.com/DynamoRIO/drmemory&#xff0c;最新发…...

J6412四网口迷你主机折腾虚拟机教程

今天给大家做一个四网口迷你主机折腾虚拟机的安装教程&#xff0c;主机采用的是maxtang大唐NUC J6412 intel i226V四网口的迷你主机&#xff0c;这款主机它是不能直接装上NAS的&#xff0c;必须使用虚拟机系统&#xff0c;近期研究了下然后做了一个教程分享给大家。 首先需要做…...

电子招标采购系统—企业战略布局下的采购寻源

​ 智慧寻源 多策略、多场景寻源&#xff0c;多种看板让寻源过程全程可监控&#xff0c;根据不同采购场景&#xff0c;采取不同寻源策略&#xff0c; 实现采购寻源线上化管控&#xff1b;同时支持公域和私域寻源。 询价比价 全程线上询比价&#xff0c;信息公开透明&#xff…...

elasticsearch 之 mapping 映射

当我们往 es 中插入数据时&#xff0c;若索引不存在则会自动创建&#xff0c;mapping 使用默认的&#xff1b;但是有时默认的映射关系不能满足我们的要求&#xff0c;我们可以自定义 mapping 映射关系。 mapping 即索引结构&#xff0c;可以看做是数据库中的表结构&#xff0c…...

2023年rabbitMq面试题汇总2(5道)

一、如何确保消息接收⽅消费了消息&#xff1f;接收⽅消息确认机制&#xff1a;消费者接收每⼀条消息后都必须进⾏确认&#xff08;消息接收和消息确认是两个不同操作&#xff09;。只有消费者确认了消息&#xff0c;RabbitMQ才能安全地把消息从队列中删除。这⾥并没有⽤到超时…...

电视剧《狂飙》数据分析,正片有效播放市场占有率达65.7%

哈喽大家好&#xff0c;春节已经过去了&#xff0c;朋友们也都陆陆续续开工了&#xff0c;小编在这里祝大家开工大吉&#xff01;春节期间&#xff0c;一大批电视剧和网剧上映播出&#xff0c;其中电视剧《狂飙》以不可阻挡之势成功成为“开年剧王”。这里小编整理了一些《狂飙…...

cas单点登录后重定向次数过多问题以及调试cas-dot-net-client

问题描述&#xff1a; web项目应用cas作为单点登录站点&#xff0c;登录后无法打开WEB项目的页面&#xff0c;报错&#xff0c;说重定向次数过多。 老实说&#xff0c;这种问题&#xff0c;以前遇到过不少&#xff0c;是我这种半桶水程序员的噩梦。解决这种问题&#xff0c;不…...

【监控】Prometheus(普罗米修斯)监控概述

文章目录一、监控系统概论二、基础资源监控2.1、网络监控2.2、存储监控2.3、服务器监控2.4、中间件监控2.5、应用程序监控&#xff08;APM&#xff09;三、Prometheus 简介3.1、什么是 Prometheus3.2、优点3.3、组件3.4、架构3.5、适用于什么场景3.6、不适合什么场景四、数据模…...

opencv+python物体检测【03-模仿学习】

仿照练习&#xff1a;原文链接 步骤一&#xff1a;准备图片 正样本集&#xff1a;正样本集为包含“识别物体”的灰度图&#xff0c;一般大于等于2000张&#xff0c;尺寸不能太大&#xff0c;尺寸太大会导致训练时间过长。 负样本集&#xff1a;负样本集为不含“识别物体”的…...

计算机科学基础知识第二节讲义

课程链接 运行环境&#xff1a;WSL Ubuntu OMZ终端 PS&#xff1a;看到老师终端具有高亮和自动补全功能&#xff0c;我连夜肝出oh-my-zsh安装教程&#xff0c;实现了此功能。 这节课主要讲变量的语法、控制流程、shell功能等内容。 修改终端用户名&#xff0c;输入密码后重启…...

openssl genrsa 命令详解

文章目录一、openssl genrsa 命令介绍二、openssl genrsa 命令的语法及选项三、实例1、生成512位的 RSA 秘钥&#xff0c;输出到屏幕。2、生成512位 RSA 私钥&#xff0c;输出到指定的文件 genrsa.txt3、生成 1024 位 RSA 秘钥&#xff0c;采用 des 算法加密&#xff0c;加密密…...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

智能仓储的未来:自动化、AI与数据分析如何重塑物流中心

当仓库学会“思考”&#xff0c;物流的终极形态正在诞生 想象这样的场景&#xff1a; 凌晨3点&#xff0c;某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径&#xff1b;AI视觉系统在0.1秒内扫描包裹信息&#xff1b;数字孪生平台正模拟次日峰值流量压力…...

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决 问题背景 在一个基于 Spring Cloud Gateway WebFlux 构建的微服务项目中&#xff0c;新增了一个本地验证码接口 /code&#xff0c;使用函数式路由&#xff08;RouterFunction&#xff09;和 Hutool 的 Circle…...

Java 二维码

Java 二维码 **技术&#xff1a;**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...

SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题

分区配置 (ptab.json) img 属性介绍&#xff1a; img 属性指定分区存放的 image 名称&#xff0c;指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件&#xff0c;则以 proj_name:binary_name 格式指定文件名&#xff0c; proj_name 为工程 名&…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...

MFC 抛体运动模拟:常见问题解决与界面美化

在 MFC 中开发抛体运动模拟程序时,我们常遇到 轨迹残留、无效刷新、视觉单调、物理逻辑瑕疵 等问题。本文将针对这些痛点,详细解析原因并提供解决方案,同时兼顾界面美化,让模拟效果更专业、更高效。 问题一:历史轨迹与小球残影残留 现象 小球运动后,历史位置的 “残影”…...

协议转换利器,profinet转ethercat网关的两大派系,各有千秋

随着工业以太网的发展&#xff0c;其高效、便捷、协议开放、易于冗余等诸多优点&#xff0c;被越来越多的工业现场所采用。西门子SIMATIC S7-1200/1500系列PLC集成有Profinet接口&#xff0c;具有实时性、开放性&#xff0c;使用TCP/IP和IT标准&#xff0c;符合基于工业以太网的…...

ubuntu22.04有线网络无法连接,图标也没了

今天突然无法有线网络无法连接任何设备&#xff0c;并且图标都没了 错误案例 往上一顿搜索&#xff0c;试了很多博客都不行&#xff0c;比如 Ubuntu22.04右上角网络图标消失 最后解决的办法 下载网卡驱动&#xff0c;重新安装 操作步骤 查看自己网卡的型号 lspci | gre…...