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. Container View Controller

    有时候,我们的Controler中包含有另一个controler view的view时,可以使用这种方式. https://developer.apple.com/library/ios/featur ...

  2. poj 1004 Financial Management

    求平均数,记得之前在杭电oj上做过一个求平均数的题目,结果因为题目是英文的,我就懵逼了 #include <stdio.h> int main() { ; double num; int ...

  3. 转】MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3544208.html 感谢! 一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制 ...

  4. SpringMVC处理Date类型的成员变量方法

    原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...

  5. java线性表学习笔记(二)

    链表中的每一个元素都包含一个称为节点的结构,每向链表中增加一个元素,就会产生一个与之相关的节点,每个节点与它相邻的节点相连接(这是基础吧,不过在看c的时候没认真看,呼). 定义节点类如下(使用了泛型, ...

  6. python svn

    svn 0.3.36 Downloads ↓ Intuitive Subversion wrapper. Introduction svn is a simple Subversion library ...

  7. JS的一些小验证

    1,文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...

  8. eclipse 最有用的10个快捷键

    1Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.    ...

  9. javascript js 内存泄露工具使用

    javascript内存泄露工具使用 原文:http://lanhy2000.blog.163.com/blog/static/43678608201121472644851/ 2011-03-14 ...

  10. Delphi ThreadPool 线程池(Delphi2009以上版本适用)

    http://blog.sina.com.cn/s/blog_6250a9df0101kref.html 在网上查找Delphi线程池,结果发现寥寥无几. 看了半天源代码,弄得一头雾水,觉得不容易理解 ...