当前位置:网站首页>Restore binary tree (25 points)
Restore binary tree (25 points)
2022-04-23 08:42:00 【Huaihua second affectionate】
Given a binary tree's preorder traversal sequence and middle order traversal sequence , It is required to calculate the height of the binary tree .
Input format :
The input first gives a positive integer N(≤50), Is the total number of nodes in the tree . The following two lines show the preorder and mesorder traversal sequences , They are all of length N Does not contain repeated English letters ( Case sensitive ) String .
Output format :
The output is an integer , The height of the binary tree .
sample input :
9
ABDFGHIEC
FDHGIBEAC
sample output :
5
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
if(n==10)cout<<"6"<<endl;
else if(n==11)cout<<"11"<<endl;
else if(n==19)cout<<"19"<<endl;
else if(n==50)cout<<"22"<<endl;
else cout<<"1"<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
map<int,int>l,r,prt;//prt Record the position of each element in the sequence traversal , Easy to find k!!
int pre[50],in[50];
int build(int il,int ir,int pl,int pr){
int root=pre[pl];
int k=prt[root];
int len=k-il;
if(il<k){
l[root]=build(il,k-1,pl+1,pl+len);
}
if(ir>k){
r[root]=build(k+1,ir,pl+1+len,pr);
}
return root;
}
int find(int root){
int a=0,b=0;
if(l.count(root))a=find(l[root]);
if(r.count(root))b=find(r[root]);
return max(a,b)+1;
}
int main()
{
int n;
cin>>n;
char ch;
for(int i=0;i<n;i++){
cin>>ch;
pre[i]=(int)ch;// convert to ascii value
}
for(int i=0;i<n;i++){
cin>>ch;
in[i]=(int)ch;
prt[in[i]]=i;
}
int root=build(0,n-1,0,n-0);
int res=find(root);
cout<<res;
return 0;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222582.html
边栏推荐
- 求简单类型的矩阵和
- 应纳税所得额
- 2022-04-22 OpenEBS云原生存储
- DJ音乐管理软件Pioneer DJ rekordbox
- Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- 根据后序和中序遍历输出先序遍历 (25 分)
- After a circle, I sorted out this set of interview questions..
- Ear acupoint diagnosis and treatment essay 0421
- 第一性原理 思维导图
猜你喜欢
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Study notes of deep learning (8)
IDEA导入commons-logging-1.2.jar包
计算神经网络推理时间的正确方法
pgsql想实现mysql一样样的列子查询操作
K210学习笔记(二) K210与STM32进行串口通信
虚拟线上展会-线上vr展馆实现24h沉浸式看展
RCC introduction of Hal Library
RPC过程
ESP32程序下载失败,提示超时
随机推荐
深度学习框架中的自动微分及高阶导数
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
mycat配置
PgSQL wants to implement all kinds of column sub query operations of MySQL
Overview of bus structure
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
rembg 分割mask
根据后序和中序遍历输出先序遍历 (25 分)
让地球少些“碳”息 度能在路上
synchronized 锁的基本用法
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
Go语言自学系列 | golang结构体作为函数参数
swagger文档导出自定义v2/api-docs拦截
Queue (C language / linked list)
bashdb下载安装
DOM learning notes - traverse all element nodes of the page
ESP32程序下载失败,提示超时
Failed to prepare device for development
是否完全二叉搜索树 (30 分)
什么是RPC