当前位置: 首页 > 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;
}

http://www.lryc.cn/news/793.html

相关文章:

  • CDN绕过技术总汇
  • 算法训练营DAY51|300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
  • mac:彻底解决-安装应用后提示:无法打开“XXX”,因为无法验证开发者的问题;无法验证此App不包含恶意软件
  • CPU 指标 user/idle/system 说明
  • Thinkphp大型进销存ERP源码/ 进销存APP源码/小程序ERP系统/含VUE源码支持二次开发
  • hgame2023 WebMisc
  • 67. Python的绝对路径
  • VHDL语言基础-组合逻辑电路-加法器
  • 内存检测工具Dr.Memory在Windows上的使用
  • J6412四网口迷你主机折腾虚拟机教程
  • 电子招标采购系统—企业战略布局下的采购寻源
  • elasticsearch 之 mapping 映射
  • 2023年rabbitMq面试题汇总2(5道)
  • 电视剧《狂飙》数据分析,正片有效播放市场占有率达65.7%
  • cas单点登录后重定向次数过多问题以及调试cas-dot-net-client
  • 【监控】Prometheus(普罗米修斯)监控概述
  • opencv+python物体检测【03-模仿学习】
  • 计算机科学基础知识第二节讲义
  • openssl genrsa 命令详解
  • C语言标准 —— C89(C90)、C99、C11、C17、C2X
  • 基于Java+Dubbo设计的智能公交查询系统
  • go语言的并发编程
  • 亚马逊要求UL94防火测试阻燃测试标准及项目
  • ClickHouse 合并树表引擎 MergeTree 原理分析
  • 用YOLOv8推荐的Roboflow工具来训练自己的数据集
  • 三层交换机【实验】
  • Anolis 8.6 部署 Kafka 3.3.1 安装和测试(二)
  • sed和awk
  • 使用STM32 CUBE IDE配置STM32F7 用DMA传输多通道ADC数据
  • linux 学习(持续更新)