当前位置:网站首页>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;
}
边栏推荐
- Record sentry's path of stepping on the pit
- 在线音乐播放器app
- Visual Studio導入
- 【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
- Zzuli:1061 sequential output of digits
- Fabric.js 3个api设置画布宽高
- 2022-2-14 learning xiangniuke project - Section 6 displays login information
- Collectors. Groupingby sort
- Fabric.js IText 手动设置斜体
- Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
猜你喜欢
Visual Studio导入
Gee series: Unit 2 explore datasets
7. Eleven state sets of TCP
Fabric.js 将本地图像上传到画布背景
Fabric.js IText 上标和下标
Fabric. JS iText set italics manually
Win10 copy files, save files... All need administrator permission, solution
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
2022-2-14 learning xiangniuke project - Section 7 account setting
随机推荐
数据的储存
Thread pool overview
Fabric.js 背景不受视口变换影响
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
Determine whether there is an element in the string type
centos8安裝mysql8.0.22教程
Mysql基础---查询(1天学会mysql基础)
LS1046nfs挂载文件系统
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Gee series: unit 8 time series analysis in Google Earth engine [time series]
Basic use of form
Fabric. JS basic brush
460. LFU 缓存 双向链表
7.1模擬賽總結
Fabric.js 自由绘制矩形
生成二维码
运维工作的“本手、妙手、俗手”
Fabric. JS background is not affected by viewport transformation
Global and Chinese market of cell culture freezers 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2-14 learning xiangniuke project - Section 7 account setting