博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA548 Tree (二叉树的遍历)
阅读量:7199 次
发布时间:2019-06-29

本文共 2233 字,大约阅读时间需要 7 分钟。

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a

path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values

of nodes along that path.

Input

The input file will contain a description of the binary tree given as the inorder and postorder traversal

sequences of that tree. Your program will read two line (until end of file) from the input file. The first

line will contain the sequence of values associated with an inorder traversal of the tree and the second

line will contain the sequence of values associated with a postorder traversal of the tree. All values

will be different, greater than zero and less than 10000. You may assume that no binary tree will have

more than 10000 nodes or less than 1 node.

Output

For each tree description you should output the value of the leaf node of a path of least value. In the

case of multiple paths of least value you should pick the one with the least value on the terminal node.

Sample Input

3 2 1 4 5 7 6

3 1 2 5 6 7 4

7 8 11 3 5 16 12 18

8 3 11 7 16 18 12 5

255

255

Sample Output

1

3

255

【分析】嗯  开始学习二叉树了,感觉很难,这个代码不是很懂,感觉像是先通过中序遍历,后序遍历求出先序遍历,找到子叶点求和。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mod 1000000007#define inf 0x3f3f3f3f#define pi acos(-1.0)using namespace std;typedef long long ll;const int N=300;const int M=15005;char s[100005];int v1[100005],v2[100005],top;int min_sum,ans;int init(char *s,int *v) { int top=0; for(int i=0; s[i]; i++) { while(s[i]==' ') i++; v[top]=0; while(s[i]&&isdigit(s[i])) { v[top]=v[top]*10+s[i]-'0'; i++; } top++; if(!s[i]) break; } return top;}int find(int *v,int n,int c) { for(int i=n-1; i>=0; i--) if(v[i]==c) return i; return 0;}void build(int n,int *v1,int *v2,int sum) { if(n<=0) return ; int p=find(v1,n,v2[n-1]); //printf("v2[n-1]=%d p=%d\n",v2[n-1],p); sum+=v2[n-1]; if(p<=0&&n-p-1<=0) { if(sum==min_sum) ans=min(ans,v2[n-1]); else if(sum
View Code

 

转载于:https://www.cnblogs.com/jianrenfang/p/5738173.html

你可能感兴趣的文章
【转】浅谈CSRF攻击方式
查看>>
-21.jpg
查看>>
出库发货扫描检测软件(方案)
查看>>
vue中axios的使用与封装
查看>>
java list几种性能比较
查看>>
学习笔记之FluentAssertions
查看>>
JavaScript高级程序设计(第三版)学习笔记11、12、17章
查看>>
B00014 C++实现的AC自动机
查看>>
条件测试与比较
查看>>
已知/未知宽高的浮动元素水平居中对齐 和 图片水平垂直居中对齐
查看>>
解决 - java.lang.OutOfMemoryError: unable to create new native thread
查看>>
f5健康检查
查看>>
玩转计划任务命令:schtasks
查看>>
[Linux] Big-endian and Little-endian (大小端模式)
查看>>
非父子组件生命周期的关系
查看>>
web crawling(plus6) pic mining
查看>>
01Hadoop简介
查看>>
树莓派上搭建唤醒词检测引擎 Snowboy
查看>>
hdu 3635 Dragon Balls(并查集技巧)
查看>>
算法和策略之间的混沌关系
查看>>