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 <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
char s[MAXN];
int mx,k;
void handle(int buf[],int &len)
{
int l=strlen(s);
int x=;
for(int i=;i<l;i++)
{
if(s[i]==' ')
{
buf[len++]=x;
x=;
}
else
{
x*=;
x+=(s[i]-'');
}
}
buf[len++]=x;
}
int in[MAXN],len1;
int post[MAXN],len2;
void build(int l1,int r1,int l2,int r2,int sum)
{
if(l1>r1)
{
return ;
}
if(l1==r1&&l2==r2)//走到叶子结点
{
int x=sum+in[l1];
if(x<mx)
{
mx=x;
k=in[l1];
}
else if(x==mx)
{
k=min(k,in[l1]);
}
} int root=post[r2];
int p=;
while(in[p]!=root) p++;
int cnt=p-l1;
build(l1,p-,l2,l2+cnt-,sum+root);
build(p+,r1,l2+cnt,r2-,sum+root);
}
int main()
{
while(cin.getline(s,MAXN))
{
len1=len2=;
handle(in,len1);
cin.getline(s,MAXN);
handle(post,len2);
mx=INF;
k=INF;
build(,len1-,,len2-,);
cout<<k<<endl;
}
return ;
}

UVA548(二叉树遍历)的更多相关文章

  1. C++ 二叉树遍历实现

    原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...

  2. python实现二叉树遍历算法

    说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...

  3. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  4. hdu 4605 线段树与二叉树遍历

    思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...

  5. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

  6. D - 二叉树遍历(推荐)

    二叉树遍历问题 Description   Tree Recovery Little Valentine liked playing with binary trees very much. Her ...

  7. 二叉树遍历 C#

    二叉树遍历 C# 什么是二叉树 二叉树是每个节点最多有两个子树的树结构 (1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层有叶子结点,并 ...

  8. 二叉树——遍历篇(递归/非递归,C++)

    二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...

  9. 二叉树遍历(flist)(二叉树,已知中序层序,求先序)

    问题 C: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 76  解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...

随机推荐

  1. Cgroups控制cpu,内存,io示例【转】

    本文转载自:https://www.cnblogs.com/yanghuahui/p/3751826.html 百度私有PaaS云就是使用轻量的cgoups做的应用之间的隔离,以下是关于百度架构师许立 ...

  2. Python 数值类型

    1.数值类型分为整形(二进制(0b),八进制(0o),十进制,十六进制(0x) ),浮点型,long,complex(复合行) 当我们说十进制数的时候,是逢10进1,就是说到达10的时候就要向前一位进 ...

  3. RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)

    . I have added two obejcts data source to Report Viewer. 2. in RDLC i have created two tables and in ...

  4. relativePath

    比如: com.tenace tenace 2.0.1 ../pom.xml //刚开始无此句 com.spider engine 2.6.0-SNAPSHOT tenace作为pom项目已经发布至r ...

  5. RHCE学习笔记 管理1 (第三~五章)

    第三章 红帽企业linux 获取帮助 (略) man .pinfo. 第四章 编辑文件 1.输出重定向到文件和程序 >file    定向文件(覆盖) >>file   定向文件(附 ...

  6. Could not fetch URL https://pypi.org/simple/pip/: There was a problem confir

    这个问题其实是无意中解决的:因为在网上找不到解决办法,是我在yum -y installl wget后,自动就好了,安装wget的时候,可能更新了openssl的缘故吧.

  7. Linux自定义别名alias重启失效问题

    Linux上的别名功能非常方便,例如ll可以显示文件列表的长信息,但是却不是以human能读懂的方式显示,所以我尝试直接在命令行中自定义一个别名: alisa lk='ls -lh' 然后lk就能正常 ...

  8. 防域名DNS劫持 从保护帐号安全做起

    什么攻击能造成区域性的网络瘫痪?没错,DNS劫持.这个堪称核武器的攻击方式,一旦爆炸,后果不堪设想.2014年1月21日,全国大范围出现DNS故障,下午,中国顶级域名根服务器出现故障,大部分网站受影响 ...

  9. JavaScript -- 练习,Dom 获取节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. mini6410基于linux2.6.36内核通过NFS启动根文件系统总结(四制作根文件系统及通过NFS挂载文件系统)

    http://blog.csdn.net/yinjiabin/article/details/7489563 根文件系统一般包括: 1)基本的文件系统结构,包含一些必须的目录,比如:/dev,/pro ...