当前位置:网站首页>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;
}
边栏推荐
- 记录sentry的踩坑之路
- Two implementation methods of delay queue
- 数据库批量插入数据
- Fabric.js IText设置指定文字的颜色和背景色
- Gee dataset: chirps pentad high resolution global grid rainfall dataset
- 小程序跳装到公众号
- "Original, excellent and vulgar" in operation and maintenance work
- KMP idea and template code
- Here comes a new chapter in the series of data conversion when exporting with easyexcel!
- Gee: create a new feature and set corresponding attributes
猜你喜欢

Fabric. JS activation input box

Visual Studio导入

Online music player app

MySQL foundation --- query (learn MySQL foundation in 1 day)

黑马笔记---Map集合体系

Disable access to external entities in XML parsing

JVM class loading mechanism
![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]

Win10 copy files, save files... All need administrator permission, solution

黑马笔记---Set系列集合
随机推荐
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
Fabric. JS iText superscript and subscript
摆正元素(带过渡动画)
黑馬筆記---Set系列集合
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
Database batch insert data
黑马笔记---Set系列集合
Zzuli:1060 numbers in reverse order
Straighten elements (with transition animation)
Generate QR code
Nodejs (03) -- custom module
"Original, excellent and vulgar" in operation and maintenance work
Fabric.js IText设置指定文字的颜色和背景色
简单封装 js并应用
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
RNN recurrent neural network
黑马笔记---Map集合体系
记录sentry的踩坑之路
Zzuli:1064 encrypted characters
运维工作的“本手、妙手、俗手”