548 - Tree (UVa OJ)
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)的更多相关文章
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- UVA - 548 Tree(二叉树的递归遍历)
题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...
- Tree(uva 536)
先声明,我还在学习中,这个题大部分代码借鉴的大佬的,其实这算是比较经典二叉树题了,关键在于递归建树. 代码附上: #include <iostream> #include <cstr ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
随机推荐
- Classic Source Code Collected
收藏一些经典的源码,持续更新!!! 1.深度学习框架(Deep Learning Framework). A:Caffe (Convolutional Architecture for Fast Fe ...
- 阿里云开放服务oss的api
http://imgs-storage.cdn.aliyuncs.com/help/oss/OSS_API_20131015.pdf?spm=5176.383663.5.23.JQjiIK&f ...
- 深刻理解Python中的元类metaclass(转)
本文由 伯乐在线 - bigship 翻译 英文出处:stackoverflow 译文:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上很热 ...
- EIGRP默认路由分发的四种方法
方式一:重发布静态 R2(config)#ip route 0.0.0.0 0.0.0.0 s1/1 R2(config)#router eig 10 R2(config-router)#redist ...
- oracle不用tsname文件的时候着怎么办
oracle\product\10.2.0\client_2\odp.net\PublisherPolicy\Policy.9.2.Oracle.DataAccess.config 找到newVers ...
- “IPSec 共享密钥”丢失。
“IPSec 共享密钥”丢失.请验证您的设置并尝试重新连接. 但是这个vpn原来使用时不需要配置 共享的密钥,而且我也没有这个共享的密钥, 搜了下Mac OSX可以修改配置来绕过它. 在/etc/pp ...
- xib上拖拽scrollview的自动布局方法
http://www.cocoachina.com/ios/20150104/10810.html
- IE6 背景透明
IE6 背景透明 第 1 种方法:定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了.(注意两处图片的路径写法不一样,本例中,icon_home.png图片与html文件在 ...
- NAND flash NOR flash SDRAM区别
nand flash:适合大容量数据存储,类似硬盘:nor flash:适合小容量的程序或数据存储,类似小硬盘:sdram:主要用于程序执行时的程序存储.执行或计算,类似内存. 区别:nor flas ...
- (剑指Offer)面试题18:树的子结构
题目: 输入两棵二叉树A和B,判断B是不是A的子结构. 二叉树结构定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; ...