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 ...
随机推荐
- git常用操作命令2
以github为例,测试本地库与远程库github之间的交互 1. 本地初始化一个git库 创建一个test文件夹,然后cd到test文件内, 执行git init命令 初始化本地库成功!! ...
- 【Linux】ssh执行远程命令awk 参数报错问题
ssh ip sudo docker ps -a | grep none | awk '{print \$1}'| sed 's/%//g' $1前面加上转移符就好
- delphi 半透明窗体类
{******************************************************************************* 半透明窗体控件 版本:1.0 功能说明 ...
- VMware vSphere(虚拟化平台)
VMware vSphere 是业界领先且最可靠的虚拟化平台.vSphere将应用程序和操作系统从底层硬件分离出来,从而简化了 IT操作.您现有的应用程序可以看到专有资源,而您的服务器则可以作为资源池 ...
- TP中如何用IF
将TP中这个容易忘的知识点记下来以便日后翻阅 $memberField = "ID, NAME, MOBILE, MEMBER_STATUS as status, IF (MEMBER_ST ...
- 搭建本地npm
cnpm install -g sinopia 然后执行sinopia npm set registry [url] npm adduser 然后就可以发布了 使用的时候切换registry就可以 修 ...
- Cent OS (二)常用的命令介绍
1. 常用的Linux命令 序号 命令 对应英文 作用 01 ls list 查看当前文件夹下的内容 02 pwd print work directory 查看当前所在的文件夹 03 cd [目 ...
- JS-for..of
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of 刚刚上网上看到<V8 ...
- Ajax二级联动简单实例
效果图: 图1 图2(浙江省内存在山东省的数据,原因是先前加入的数据未删除) 思路:通过下拉省份,将省份id传入后台,根据省份塞入相应省份的市的数据,将市的数据再次传回前端 前端HTML及JS代码: ...
- 重新认识Maven
PS:第一次接触maven大约是两年前吧,隐约记得之前都是人工寻找并下载很多jar,放在项目的lib中(表示太年轻,没有接触过Ant或者其他类似的工具,就不找别人写的比较了).懒人永远有着自己的小聪明 ...