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 ...
随机推荐
- ado无法访问数据库问题
现象:以ADO方式访问数据库的C++程序,在一台计算机上能访问成功,在另一台计算机上却访问不成功,报告不能连接错误,并且这两台计算机都装有ado. 原因:ado版本不对 解决方案:下载KB983246 ...
- poj 1004 Financial Management
求平均数,记得之前在杭电oj上做过一个求平均数的题目,结果因为题目是英文的,我就懵逼了 #include <stdio.h> int main() { ; double num; int ...
- 测试peerdroid示例程序步骤
来自JXTA交流群(36855950)...韦发改(992611244) 15:12:25—————————————————————————————————————————————————————— ...
- Linux下的paste合并命令详解
paste单词意思是粘贴.该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同.paste将按行将不同文件行信息放在一 ...
- vs常用插件之javsscript插件
1.JSEnhancements 折叠JS和CSS代码 http://visualstudiogallery.msdn.microsoft.com/0696ad60-1c68-4b2a-9646-4b ...
- 关于 三星 I9100 (水货)
前天陪好友去买水货9100,总结了一点经验,觉得挺有用的,今天整理一下写出来...有 需要的可以看看..原创整理.. 一,当然是检查外观(检查USB接口有没有磨损,检查摄像头是否有灰尘,检查屏幕是不是 ...
- 3.emWin5.26(ucGui)VS2008 2-D图形库-基本绘图【Worldsing笔记】
UCGUI(emWin) 2-D图形库--之基本板绘图,在ucgui的基本绘图功能上来看,功能还是比较全的,本例程主要使用基本的接口,两个主要的概念是绘制(draw)和填充(Fill),这两的区别是一 ...
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
- UVaLive 6698 Sightseeing Bus Drivers (水题,贪心)
题意:n个工人,有n件工作a,n件工作b,每个工人干一件a和一件b,a[i] ,b[i]代表工作时间,如果a[i]+b[j]>t,则老板要额外付钱a[i]+b[j]-t;现在要求老板付钱最少: ...
- CStdioFile
CStdioFile类的声明保存再afx.h头文件中. CStdioFile类继承自CFile类,CStdioFile对象表示一个用运行时的函数fopen打开的c运行时的流式文件.流式文件是被缓冲的, ...