当前位置: 首页 > 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;加密密…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…...

Nuxt.js 中的路由配置详解

Nuxt.js 通过其内置的路由系统简化了应用的路由配置&#xff0c;使得开发者可以轻松地管理页面导航和 URL 结构。路由配置主要涉及页面组件的组织、动态路由的设置以及路由元信息的配置。 自动路由生成 Nuxt.js 会根据 pages 目录下的文件结构自动生成路由配置。每个文件都会对…...

Psychopy音频的使用

Psychopy音频的使用 本文主要解决以下问题&#xff1a; 指定音频引擎与设备&#xff1b;播放音频文件 本文所使用的环境&#xff1a; Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...

MySQL 8.0 OCP 英文题库解析(十三)

Oracle 为庆祝 MySQL 30 周年&#xff0c;截止到 2025.07.31 之前。所有人均可以免费考取原价245美元的MySQL OCP 认证。 从今天开始&#xff0c;将英文题库免费公布出来&#xff0c;并进行解析&#xff0c;帮助大家在一个月之内轻松通过OCP认证。 本期公布试题111~120 试题1…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现录音机应用

1. 项目配置与权限设置 1.1 配置module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.MICROPHONE","reason": "录音需要麦克风权限"},{"name": "ohos.permission.WRITE…...

html-<abbr> 缩写或首字母缩略词

定义与作用 <abbr> 标签用于表示缩写或首字母缩略词&#xff0c;它可以帮助用户更好地理解缩写的含义&#xff0c;尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时&#xff0c;会显示一个提示框。 示例&#x…...

Typeerror: cannot read properties of undefined (reading ‘XXX‘)

最近需要在离线机器上运行软件&#xff0c;所以得把软件用docker打包起来&#xff0c;大部分功能都没问题&#xff0c;出了一个奇怪的事情。同样的代码&#xff0c;在本机上用vscode可以运行起来&#xff0c;但是打包之后在docker里出现了问题。使用的是dialog组件&#xff0c;…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...

windows系统MySQL安装文档

概览&#xff1a;本文讨论了MySQL的安装、使用过程中涉及的解压、配置、初始化、注册服务、启动、修改密码、登录、退出以及卸载等相关内容&#xff0c;为学习者提供全面的操作指导。关键要点包括&#xff1a; 解压 &#xff1a;下载完成后解压压缩包&#xff0c;得到MySQL 8.…...

鸿蒙(HarmonyOS5)实现跳一跳小游戏

下面我将介绍如何使用鸿蒙的ArkUI框架&#xff0c;实现一个简单的跳一跳小游戏。 1. 项目结构 src/main/ets/ ├── MainAbility │ ├── pages │ │ ├── Index.ets // 主页面 │ │ └── GamePage.ets // 游戏页面 │ └── model │ …...