当前位置:网站首页>Uva548 tree
Uva548 tree
2022-07-02 05:32:00 【Harmful Poems】
Enter the middle order and post order traversal of a binary tree , Please output a leaf node , The sum of values from the leaf node to the root is the smallest , And this leaf is the one with the smallest number . Input : Your program will read two lines from the input file ( Until the end of the document ). The first line is the middle order traversal value sequence of the tree , The second line is the sequence of traversal values after the tree . All values will be different , Greater than zero and less than or equal to 10000. A section of a binary tree 1<=N<=10000. Output : For each tree description , You should output the value of the leaf node of the minimum path . In the case of minimum multipath , You should select the path with the minimum value on the terminal leaf node , And output the terminal leaf of the minimum value .
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000+5;
int order[maxn],postorder[maxn],lch[maxn],rch[maxn];
int n,minv,minsum;
// Traversing sequence to establish binary tree
int createtree(int l1, int l2, int m){
if(m <= 0){
return 0;
}
int root = postorder[l2+m-1];
int len = 0;
while(inorder[l1+len] != root)// Calculate the length of the left subtree
len++;
lch[root] = createtree(l1,l2,len);
rch[root] = createtree(l1+len+1,l2+len,m-len-1);
return root;
}
bool readline(int *a){
// Read the traversal sequence , There's a space in the middle
string line;
if(!getline(cin,line))
return false;
stringstream s(line);
n = 0;
int x;
while(s>>x){
a[n++] = x;
return n > 0;
}
}
void findmin(int v,int sum){
sum += v;
if(!lch[v] && !rch[v])// leaf
if(sum < minsum || (sum == minsum&& v<minv)){
minv = v;
minsum = sum;
}
if(lch[v]) //v There's a Zuozi tree
findmin(lch[v],m);
if(rch[v])
findmin(rch[v],m);
}
int main(){
while(readline(inorder)){
readline(postorder);
createtree(0,0,n);
minsum = 0x7fffffff;
findmin(postorder[n-1],0);
cout<<minv<<endl;
}
return 0;
}
边栏推荐
- Gee: create a new feature and set corresponding attributes
- 7.TCP的十一种状态集
- Fabric. JS activation input box
- Leetcode 18 problem [sum of four numbers] recursive solution
- Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
- Fabric. JS basic brush
- 黑马笔记---Set系列集合
- Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
- [personal test] copy and paste code between VirtualBox virtual machine and local
- Gee series: unit 9 generate sampling data in GEE [random sampling]
猜你喜欢
idea開發工具常用的插件合集匯總
Fabric.js IText 上标和下标
Basic use of form
视差特效的原理和实现方法
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
Gee: use of common mask functions in remote sensing image processing [updatemask]
Gee: create a new feature and set corresponding attributes
Gee dataset: chirps pentad high resolution global grid rainfall dataset
2022-2-15 learning xiangniuke project - Section 8 check login status
centos8安装mysql8.0.22教程
随机推荐
Disable access to external entities in XML parsing
Brew install * failed, solution
Mysql基础---查询(1天学会mysql基础)
ERP management system development and design existing source code
中小型项目手撸过滤器实现认证与授权
Fabric. JS iText sets the color and background color of the specified text
"Original, excellent and vulgar" in operation and maintenance work
Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table
A collection of commonly used plug-ins for idea development tools
函数栈帧的创建和销毁
Financial portal related information
Thread pool overview
Dark horse notes -- map set system
Foreign trade marketing website system development function case making
黑馬筆記---Set系列集合
With an amount of $50billion, amd completed the acquisition of Xilinx
记录sentry的踩坑之路
简单封装 js并应用
h5跳小程序
Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share