当前位置:网站首页>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;
}
边栏推荐
- Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
- Reflection of the soul of the frame (important knowledge)
- ubuntu20.04安装mysql8
- Fabric.js IText 上标和下标
- Fabric. JS activation input box
- Gee: create a new feature and set corresponding attributes
- Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
- Gee series: Unit 1 Introduction to Google Earth engine
- Brew install * failed, solution
- h5跳小程序
猜你喜欢
![Gee series: unit 7 remote sensing image classification using GEE [random forest classification]](/img/01/ba9441b7b1efaed85c464316740edb.jpg)
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]

Fabric. JS right click menu
![Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]](/img/25/a726643b96b6f7dbfee3782c4905d9.jpg)
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]

Online music player app

Straighten elements (with transition animation)

记录sentry的踩坑之路

idea开发工具常用的插件合集汇总

Detailed explanation of Pointer use

Fabric. JS upload local image to canvas background

centos8安裝mysql8.0.22教程
随机推荐
H5 jump applet
Fabric. JS iText superscript and subscript
Foreign trade marketing website system development function case making
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
7.1 Résumé du concours de simulation
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
Disable access to external entities in XML parsing
黑馬筆記---Set系列集合
Online English teaching app open source platform (customized)
7. Eleven state sets of TCP
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
Applet jumps to official account
Fabric. JS basic brush
Draw a wave chart_ Digital IC
Gee: remote sensing image composite and mosaic
XSS basic content learning (continuous update)
Fabric. JS activation input box
Storage of data
idea开发工具常用的插件合集汇总
Gee series: Unit 1 Introduction to Google Earth engine