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.

Input

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.

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<cstdio>
#include<cstdlib>
int mins=1<<30,minv=1<<30;
using namespace std;
struct node
{
int v;
int sum;
node* lchild;
node* rchild;
};
int in[10010],post[10010],pre[10010];
node* build(int n, int *post, int *in, node *u, int s)
{
int i=0;
if (n<=0) return NULL;
while(*(in+i)!=*post) i++;
u=(node*) malloc (sizeof(node));
u->v=*(in+i);
u->sum=s+u->v;
u->lchild=build(i,post-n+i,in,u->lchild,u->sum);
u->rchild=build(n-i-1,post-1,in+i+1,u->rchild,u->sum);
if (u->lchild==NULL && u->rchild==NULL && u->sum<=mins)
{
if (mins>u->sum)
{
mins=u->sum;
minv=u->v;
}
else if (u->v<minv)
{
minv=u->v;
}
}
return u;
}
void preorder(node* u)
{
if (u!=NULL)
{
cout<<u->v<<" "<<u->sum<<" ";
preorder(u->lchild);
preorder(u->rchild);
}
}
int main ()
{
int t,ni=0,i;
char ch;
while(scanf("%d",&t)!=EOF)
{
ch=getchar();
in[ni++]=t;
if (ch=='\n')
{
mins=minv=1<<30;
for (i=0; i<ni; i++)
cin>>post[i];
node* root;
root=build(ni,post+ni-1,in,root,0);
//preorder(root);
//cout<<endl;
cout<<minv<<endl;
ni=0;
}
}
return 0;
}

548 - Tree (UVa OJ)的更多相关文章

  1. UVa 548 Tree (建树+前序后序)

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

  2. UVa 548 Tree(中序遍历+后序遍历)

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

  3. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

  4. UVA - 548 Tree(二叉树的递归遍历)

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  5. Tree(uva 536)

    先声明,我还在学习中,这个题大部分代码借鉴的大佬的,其实这算是比较经典二叉树题了,关键在于递归建树. 代码附上: #include <iostream> #include <cstr ...

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

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

  7. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  8. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  9. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

随机推荐

  1. HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与 ...

  2. 【转】Hive学习路线图

    原文博客出自于:http://blog.fens.me/hadoop-hive-roadmap/ 感谢! Hive学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Ha ...

  3. CISCO3560 VLAN配置实例

    1.注意事项 1.1.交换机启动需要大约4-5分钟: 1.2.网线插入交换机接口从黄变为绿需要大约1-2分钟,即进入正常工作模式: 1.3.建议使用XP系统进行操作,2003默认没有安装超级终端,需要 ...

  4. HTML5简介及HTML5的发展前景

    WEB技术发展越来越迅速,HTML5的到来更是把WEB技术推向了巅峰,目前HTML5技术已经日趋成熟,不仅在PC段,HTML5更是在移动终端上也有广泛的应用,HTML5的未来十分光明,值得我们去学习. ...

  5. POJ 3298 Antimonotonicity (思维)

    题目链接:http://poj.org/problem?id=3298 找一个最长不要求连续的子序列,如a1 > a3 < a6 > a7 ... 举个例子模拟一下差不多明白了,a[ ...

  6. Android 通过ViewFlipper实现广告轮播功能并可以通过手势滑动进行广告切换

    为了实现广告轮播功能,在网上找了很多方法,有的效果很好,但是代码太麻烦,并且大多是用的viewpager,总之不是很满意. 于是看了一下sdk有个控件是ViewFlipper,使用比较方便,于是尝试了 ...

  7. android开发中关于VersionCode和VersionName

    Google为APK定义了两个关于版本属性:VersionCode和VersionName,他们有不同的用途. VersionCode:对消费者不可见,仅用于应用市场.程序内部识别版本,判断新旧等用途 ...

  8. Linux下生成动态链接库是否必须使用 -fPIC 的问题[转]

    在 Linux 下制作动态链接库,“标准” 的做法是编译成位置无关代码(Position Independent Code,PIC),然后链接成一个动态链接库.经常遇到的一个问题是 -fPIC 是不是 ...

  9. 图片 文字 input等垂直居中对齐

    span::before { width:100%; height:1px; font-size:1em; content:''; background:#fff; position:absolute ...

  10. $( document ).ready()&$(window).load()

    $( document ).ready() https://learn.jquery.com/using-jquery-core/document-ready/ A page can't be man ...