J - Tree

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Appoint description: 
System Crawler  (2014-05-16)

Description

 Tree 

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

Miguel A. Revilla

1999-01-11

题意:给你二叉树的中序与后序,求从根到叶子全部值之和最小的叶子的值。

依据中序和后序递归建树。直接遍历就可以。

#include<stdio.h>
#include<cstring>
int a[10001],b[10001];
int M,v;
struct tree
{
int date;
tree *l,*r;
tree()
{
date=0;
l=r=NULL;
}
};
tree* built(int *A,int *B,int n)
{
if(!n)return NULL;
tree *now=new tree;
int i=0;
for(i=0;i<n;i++)if(A[i]==B[0])break;//找到根在中序遍历中的位置
if(i>0)now->l=built(A,B+n-i,i);//递归建立左子树
if(i<n-1)now->r=built(A+i+1,B+1,n-i-1);//递归建立右子树
now->date=B[0];
return now;
}
void del(tree *p)
{
if(!p)return;
if(p->l)del(p->l);
if(p->r)del(p->r);
delete p;
p=NULL;
}
void dfs(tree *Root,int sum)
{
if(Root==NULL)return ;
//printf("%d ",Root->date);
if(Root->l==NULL&&Root->r==NULL)
{
if(sum+Root->date<M)
{
M=sum+Root->date;
v=Root->date;
}
}
dfs(Root->l,sum+Root->date);
dfs(Root->r,sum+Root->date);
}
int main()
{
char ch;
tree *root;
//freopen("in.txt","r",stdin);
while(~scanf("%d",&a[0]))
{
root=NULL;
int n=1;
M=100000001;
v=0;
while((ch=getchar())!='\n')
{
scanf("%d",a+n);++n;
}
for(int i=n-1;i>=0;i--)
{
scanf("%d",b+i);
}
root=built(a,b,n);
dfs(root,0);
printf("%d\n",v);
del(root);
}
return 0;
}

UVA 548(二叉树重建与遍历)的更多相关文章

  1. UVa 548 (二叉树的递归遍历) Tree

    题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...

  2. Uva 548 二叉树的递归遍历lrj 白书p155

    直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...

  3. Trees on the level UVA - 122 (二叉树的层次遍历)

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  4. UVA - 548 根据中序遍历和后序遍历建二叉树(关于三种遍历二叉树)

    题意: 同时给两个序列,分别是二叉树的中序遍历和后序遍历,求出根节点到叶子结点路径上的权值最小和 的那个 叶子节点的值,若有多个最小权值,则输出最小叶子结点的和. 想法: 一开始想着建树,但是没有这样 ...

  5. UVa 122 (二叉树的层次遍历) Trees on the level

    题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先 ...

  6. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  7. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

  8. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  9. 二叉树的递归遍历 The Falling Leaves UVa 699

    题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...

随机推荐

  1. 强制关机后导致VBOX(4.2.16 r86992)的虚拟机不可使用问题的解决MEMO

    上周六晚上由于有急事,就强制关机,导致今天晚上用VirtualBox(4.2.16 r86992)时,虚拟机上写着不可使用. 显示异常Message如下: D:\tinderbox\win-4.2\s ...

  2. HTML5 学习笔记 应用程序缓存

    使用html5 通过创建cache manifest文件,可以轻松地创建web应用的离线版本. html5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓 ...

  3. 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete

    一.类型转换运算符 必须是成员函数,不能是友元函数 没有参数 不能指定返回类型 函数原型:operator 类型名();  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  4. 在Cocos2d-X中使用xml

    XML就可以扩展标记语言.在游戏开发中,经常使用于保存游戏信息,如最高分,游戏等级.等信息,和描写叙述一些资源等,我第一次使用xml是在使用CCAnimation创建动画中,使用plist文件载入动画 ...

  5. Linux下从视频提取音频的方法

    Linux下可以利用mencoder将视频里的音频提取出来.方法如下: 1.首先安装mencoder.对于Ubuntu来说,软件仓库里就有mencoder,可直接输入如下命令安装 sudo apt-g ...

  6. JEECG 简单实例讲解权限控制

    业务背景:某公司要实现一个日志系统,用来了解员工的工作量饱和情况. 需求: 1.角色分为:员工.经理 两种. 2.员工每天在日志系统中填报工作总结,然后经理进行点评. 3.表单内容包含:姓名.日期.工 ...

  7. plink参数说明

    Plink: command-line connection utilityRelease 0.67Usage: plink [options] [user@]host [command]       ...

  8. MySQL慢查询查找和调优测试

    MySQL慢查询查找和调优测试,接下来详细介绍,需要了解的朋友可以参考下.本文参考自:http://www.jbxue.com/db/4376.html  编辑 my.cnf或者my.ini文件,去除 ...

  9. vim-程序员的利器

    个人觉得vi使用熟练后就离不开了,用了它效率会提升不少,但是没了它可能还赶不上以前的速度,给惯坏了. 以下是本人无耻的复制和粘贴的:(附图一张方便学习) Vim目前已经有各主流系统的版本,尽管vim较 ...

  10. js控制伪元素样式

    //获取伪元素// CSS代码 #myId:before { content: "hello world!"; display: block; width: 100px; heig ...