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. Unity Json 之三

    今天在网上看到一个simplejson,直接调用这两个API就可以了,简单易用 string jsonstr = SimpleJson.SimpleJson.SerializeObject(json) ...

  2. 修改redhat默认显示语言为中文

    [delmore@localhost Desktop]$ su                               //切换到最高权限 Password:                    ...

  3. 第二节课-Data-driven approach:KNN和线性分类器分类图片

    2017-08-12 1.图片分类是很多CV任务的基础: 2.图片分类要面临很多的问题,比如图片被遮挡,同一种动物有很多种颜色,形状等等,算法需要足够强壮: 3.所以很难直接写出程序来进行图片分类,常 ...

  4. 【bzoj3747】Kinoman[POI2015](线段树)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3747 对于这种题,考虑固定区间的右端点为r,设区间左端点为l能取得的好看值总和为a[l] ...

  5. HDFS-查看文件属性+文件名称过滤

    package com.zhen.hdfs; import java.io.IOException; import java.io.OutputStream; import java.net.URI; ...

  6. RPM和yum相关

    写在前面: 在这里可以知道rpm和yum的基本用法,找到更新本地yum源.搭建yum源的方法以及yum很琐碎的东西,包括yum源的优先级.用yum来安装或卸载CentOS图形界面包以及保存yum下载的 ...

  7. db2数据导出导入del与ixf格式区别

    之前做数据迁移的时候遇到乱码的一些坑,总结一下.  一般导入导出: db2 export to /home/xxxx.del of del select * from tablename db2 im ...

  8. linux防火墙开关

    对于普通Linux机器开关防火墙命令: 1重启后生效 chkconfig iptables off chkconfig iptables on 2即时生效 serivce iptables statu ...

  9. codeforces 777C.Alyona and Spreadsheet 解题报告

    题目链接:http://codeforces.com/problemset/problem/777/C 题目意思:给出一个 n * m 的矩阵,然后问 [l, r] 行之间是否存在至少一列是非递减序列 ...

  10. lightoj1422 区间dp

    对于这一题想了很久真的是一点头绪也没有,还有组数明明是200,数据范围100,O(n^3)的复杂度居然不会爆掉(可能是因为一直在想怎么用O(n^2)的复杂度做这题 做法是先预处理dp,对于dp[i][ ...