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. Unity3D游戏开发之SQLite让数据库开发更简单

    各位朋友大家好.欢迎大家关注我的博客,我是秦元培,我是博客地址是http://blog.csdn.net/qinyuanpei.在经历了一段时间的忙碌后,博主最终有时间来研究新的东西啦,今天博客向和大 ...

  2. Linux-正则表达式的POSIX规范及流派

    Linux/Unix工具与正则表达式的POSIX规范 对正则表达式有基本了解的读者,一定不会陌生『\d』.『[a-z]+』之类的表达式,前者匹配一个数字字符,后者匹配一个以上的小写英文字母.但是如果你 ...

  3. Android屏幕计量单位详解

    1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px会比较多.通常不推荐使用这个. 2.dip或dp(与密度无关的像素):一个基于density的抽象单位,这 ...

  4. php简单混淆类加密文件如何解密?

    最近在整理单位购买的源码时,发现源码里好多文件都混淆加密了.虽然不解密也不影响使用,但是心里总觉得有些别扭,便试着将加密的文件解密. 首先,百度了一下,看网上是否有现成的混淆类解密工具,搜到了一个ht ...

  5. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  6. 连接oracle时报错:ORA-28001: the password has expired

    调试Web项目的时候出现异常: java.sql.SQLException: ORA-28001: the password has expired 网上查了一下,是Oracle11g密码过期的原因 ...

  7. MySQL5.0存储过程教程

    Introduction 简介 MySQL 5.0 新特性教程是为需要了解5.0版本新特性的MySQL老用户而写的.简单的来说是介绍了“存储过程.触发器.视图.信息架构视图”,在此感谢译者陈朋奕的努力 ...

  8. 使用 ChromaKey 滤镜进行抠图

    简介 Nokia Imaging SDK  1.0 中新提供的 ChromaKey 滤镜是一个神奇的滤镜,它的基本原理就是把 一个指定范围值内的颜色变为透明或半透明,比如下面的 demo 演示的,看上 ...

  9. FileInputStream与FileOutputStream类

    FileInputStream和FileOutputStream类分别用来创建磁盘文件的输入流和输出流对象,通过它们的构造函数来指定文件路径和文件名. 创建FileInputStream实例对象时,指 ...

  10. Bootstrap学习笔记(9)--模态框(登录/注册弹框)

    说明: 1. 上来一个ul先把登录和注册两个链接扔进去,ul的类nav,navbar-nav是导航条,navbar-right让他固定在右侧.每个li的里面,data-toggle="mod ...