UVa 548 Tree (建树+前序后序)
Description
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
13
255
题目的意思是给你一个树的中序和后序遍历的情况,让你找出从根节点走到叶子节点经过的节点的权值和最小,输出这条路径的叶子节点的权值,如果最小的路径有多种则输出权值最小的叶子节点。
这个题输入处理比较奇葩,网上找了一个模板,用来读入,然后就是普通的递归建树,最后再dfs就行了。
代码如下:
#include <bits/stdc++.h> using namespace std;
#define M 100050
#define inf 0x3f3f3f3f
struct node
{
struct node *left;
struct node *right;
int v;
};
node *root;
int in[M],post[M],top,min_sum,ans;
char s[M];
int find(int *a,int n,int c)
{
for (int i=n-;i>=;i--)
if (a[i]==c)
return i;
return ;
}
node *build(int n,int *in,int *post)
{
if (n<=)
return NULL;
int p=find(in,n,post[n-]);
node *root=(node *)malloc(sizeof(node));//将malloc(分配空间)的返回值强转成 node *型指针变量
root->v=post[n-];
root->left=build(p,in,post);
root->right=build(n-p-,in+p+,post+p);
return root;
}
void dfs (node *root,int sum)
{
if (root==NULL)
return;
sum+=root->v;
if (root->left==NULL&&root->right==NULL)
{
if (min_sum>sum)
{
min_sum=sum;
ans=root->v;
}
else if (min_sum==sum)
ans=min(ans,root->v);
return ;
}
dfs(root->left,sum);
dfs(root->right,sum);
}
int init (char *s,int *a)
{
int top=;
for (int i=;s[i];++i)
{
while (s[i]==' ')
i++;
a[top]=;
while (s[i]&&isdigit(s[i]))
{
a[top]=a[top]*+s[i]-'';
++i;
}
top++;
if (!s[i])
break;
}
return top;
}
int main()
{
//freopen("de.txt","r",stdin);
while (gets(s))
{
init(s,in);
gets(s);
top=init(s,post);
node *root;
root=build(top,in,post);
min_sum=inf;
ans=min_sum;
dfs(root,);
printf("%d\n",ans);
}
return ;
}
UVa 548 Tree (建树+前序后序)的更多相关文章
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- uva 548 Tree(通过后序,先序重建树+dfs)
难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...
- UVA 548 Tree 建树
题意: 输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小. 思路: 输入后建树,然后dfs求最小的叶子. #include<iostream> #include<cstdi ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- DS Tree 已知后序、中序 => 建树 => 求先序
注意点: 和上一篇的DS Tree 已知先序.中序 => 建树 => 求后序差不多,注意的地方是在aftorder中找根节点的时候,是从右往左找,因此递归的时候注意参数,最好是拿纸和笔模拟 ...
- Tree Recovery(前序中序求后序)
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14640 Accepted: 9091 De ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVa 548 Tree(中序遍历+后序遍历)
给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...
- DS Tree 已知先序、中序 => 建树 => 求后序
参考:二叉树--前序和中序得到后序 思路历程: 在最初敲的时候,经常会弄混preorder和midorder的元素位置.大体的思路就是在preorder中找到根节点(根节点在序列的左边),然后在mid ...
随机推荐
- 10.14 socket 网络编程
简单的例子 socket客户端 import socket client = socket.socket() #声明socket类型,同时生成socket连接对象 client.connect(('l ...
- BZOJ 2547: [Ctsc2002]玩具兵(二分答案+二分图匹配)
传送门 解题思路 可以发现天兵不用管,答案的一个上界是\(2*k\),就是天兵一个个换.刚开始写了个拆\(6\)点的网络流,调了半天发现自己假了..说说正解,首先可以发现交换士兵其实就是种类的交换,那 ...
- python-zx笔记2-help
在cmd运行 查看模块的方法: help—查看模块的函数 1 help()
- Sublime Text 3 快捷键总结(Mac)
Command + Shift + L 光标同时定位多行 Command + Enter 在下一行插入新行.举个栗子:即使光标不在行尾,也能快速向下插入一行.
- Nuget-Doc:NuGet 介绍
ylbtech-Nuget-Doc:NuGet 介绍 NuGet 是适用于 .NET 的包管理器. 它使开发人员能够创建.共享和使用有用的 .NET 库. NuGet 客户端工具可生成这些库并将其作为 ...
- 后台处理json数据
InputStream in = request.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamRea ...
- sql server中实现mysql的find_in_set函数和group_concat类似功能的讲解
charindex(',' + ' test '+ ',' , ',' + test2+ ',')>0 灵活运用 SQL SERVER FOR XML PATH FOR XML ...
- Tecplot 360 安装后弹出“Is your Tecplot 360 EX liense valid?”解决方法
在hosts文件中添加127.0.0.1 download.tecplot.com这句指令时,应注意1与download之间有空格!
- Infinity、-Infinity和NaN
首先看看这三个代表什么: Infinity:正无穷大 -Infinity:负无穷大 NaN:Not a Number 当float或double类型的数除零时, 当被除数为非零值时,结果为无穷大 当被 ...
- shell 删除隐藏文件.svn
参考:https://blog.csdn.net/zhangxinrun/article/details/6409125 echo "recursively removing .svn fo ...