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. html文件转换成pdf和word

    1.html文件转成pdf 采用jar包有itext-asian.jar.itextpdf-5.5.5.jar.itext-pdfa-5.5.5.jar.itext-xtra-5.5.5.jar,为了 ...

  2. PHP获取MySQL执行sql语句的查询时间

    //计时开始 runtime(); //执行查询 mysql_query($sql); //计时结束. echo runtime(1); //计时函数 function runtime($mode=0 ...

  3. mysql删除重复记录

    Solution 1: Add Unique Index on your table: ALTER IGNORE TABLE `TableA` ADD UNIQUE INDEX (`member_id ...

  4. LeetCode——same-tree

    Question Given two binary trees, write a function to check if they are equal or not. Two binary tree ...

  5. html里id和name的异同

    id与name的作用,作为标签的标识符,基本上是一样的. name是老方法,id是在name基础上发明的,比name“现代化”一点,用的范围广一点 <...>中的name原来(刚发明时)就 ...

  6. 51nod 1060 最复杂的数 反素数

    1060 最复杂的数 基准时间限制:1 秒 空间限制:131072 KB 把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数. 例如:12的约数为:1 2 3 4 6 ...

  7. 3DES双倍长加密

    import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import ...

  8. Bellman-Ford算法 O(NE)

    Bellman-Ford算法 O(NE) 思路:枚举n-1次所有边,通过枚举所有边,将所有和已知点相连的点都设为已知,初始时起点为已知点. ;i<=n-;i++){ //n-1是次数,枚举n-1 ...

  9. stanford推荐阅读目录

    stanford deep learning 网站上推荐的阅读目录: UFLDL Recommended Readings   If you're learning about UFLDL (Unsu ...

  10. 我总结的call()与apply()方法的区别

    [call()与apply()的区别]在ECMAScript中每一个函数都是function类型(是javascript的基本引用类型)的实例,具有一定的属性和方法.call()和apply()则是这 ...