UVA548(二叉树遍历)
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(二叉树遍历)的更多相关文章
- C++ 二叉树遍历实现
原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...
- python实现二叉树遍历算法
说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- hdu 4605 线段树与二叉树遍历
思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...
- poj2255 (二叉树遍历)
poj2255 二叉树遍历 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descripti ...
- D - 二叉树遍历(推荐)
二叉树遍历问题 Description Tree Recovery Little Valentine liked playing with binary trees very much. Her ...
- 二叉树遍历 C#
二叉树遍历 C# 什么是二叉树 二叉树是每个节点最多有两个子树的树结构 (1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层有叶子结点,并 ...
- 二叉树——遍历篇(递归/非递归,C++)
二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...
- 二叉树遍历(flist)(二叉树,已知中序层序,求先序)
问题 C: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 76 解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...
随机推荐
- MySQL-LRU_List Free_List Flush_List
关于 LRU_List ,Free_List,Flush_List的介绍: LRU算法:(Latest Recent Used)最近最少使用 数据库的缓冲池通过LRU算法来进行管理. ...
- requests.post处理Content-Type: multipart/form-data的请求
前几天遇到一个需求,要调用一个接口发送请求,抓包之后得到的数据是这样的 上网看了一些资料得知,原来这个接口的数据是通过multipart/form-data格式传过去的,multipart/form- ...
- MySQL/MariaDB数据库备份与恢复之mysqlpump入门操作
创建测试用表:MariaDB [music]> create table summary(id int,info char(128));Query OK, 0 rows affected (0 ...
- linux 上传scp 压缩tar命令
1.Linux 上传scp 1)上传文件与文件夹 scp file weblogic@xx.xxx.xxx.xxx:~/songjd/ scp -r filefolder weblogic@xxx.x ...
- eval 加密 js,把js代码重新编续成新的代码,然后eval运行
eval( function(p, a, c, k, e, r) { e = function(c) { return c.toString(a) //35 }; if (!''.replace(/^ ...
- SSH或者SSM开发web,mysql数据库,数据库配置文件配置不当~数据库读写数据乱码问题解决办法。
相信,大家都有遇到过在传入一个中文string,debug自己的每一行代码时,都发现始终是没有乱码的(即:排除了,源码文件的编码格式是没问题的),但是数据进入数据库之后就是乱掉了. 那么很明显问题就出 ...
- hdoj1004--Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- 0.00-050613_boot.s
! boot.s ! ! It then loads the system at 0x10000, using BIOS interrupts. Thereafter ! it disables al ...
- Tensorflow中的命名空间scope
1.name_scope 在tensorflow中有两种声明变量的方式,tf.get_variable()和tf.Variable(). name_scope对于tf.get_variable()无效 ...
- jquery jsonp模版
$.ajax({ dataType: "jsonp", url: "http://www.b.com/b.php", jsonp: "callback ...