当前位置:网站首页>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;
}
边栏推荐
- Zzuli:1060 numbers in reverse order
- Fabric. JS gradient
- Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
- Online English teaching app open source platform (customized)
- Global and Chinese market of impact roll 2022-2028: Research Report on technology, participants, trends, market size and share
- h5跳小程序
- Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
- Using QA band and bit mask in Google Earth engine
- 黑馬筆記---Set系列集合
- Fabric. JS round brush
猜你喜欢
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
Detailed explanation of Pointer use
JVM class loading mechanism
Ls1046nfs mount file system
MySQL foundation --- query (learn MySQL foundation in 1 day)
函数栈帧的创建和销毁
Collectors. Groupingby sort
6.网络-基础
el form 表单validate成功后没有执行逻辑
7.TCP的十一种状态集
随机推荐
函数栈帧的创建和销毁
Installation du tutoriel MySQL 8.0.22 par centos8
Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
Gee: use of common mask functions in remote sensing image processing [updatemask]
460. LFU 缓存 双向链表
Fabric.js 渐变
Straighten elements (with transition animation)
Nodejs (03) -- custom module
Draw a wave chart_ Digital IC
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
Black Horse Notes - - set Series Collection
Fabric.js 将本地图像上传到画布背景
Fabric.js 3个api设置画布宽高
2022-2-15 learning xiangniuke project - Section 8 check login status
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Fabric. JS round brush
Gee dataset: chirps pentad high resolution global grid rainfall dataset
简单封装 js并应用