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 ...
随机推荐
- PHP curl_copy_handle函数
curl_copy_handle — 复制一个cURL句柄和它的所有选项 说明 resource curl_copy_handle ( resource $ch ) 复制一个cURL句柄并保持相同的选 ...
- JS中的立即执行函数
JS 立即执行函数可以让函数在创建后立即执行,这种模式本质上就是函数表达式(命名的或者匿名的),在创建后立即执行. 1.立即执行函数的写法 立即执行函数通常有下面两种写法: //第一种写法 (func ...
- basic play
[root@wen ~]# w 19:01:27 up 1 day, 7:06, 3 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN ...
- php简易分词
http://www.xunsearch.com/ 示例 http://www.xunsearch.com/scws/demo/v48.php
- 对于vue绑定的model值里边get和set的小动作
先看下例子: template里边内容: <el-form-item label="导航条类型"> <el-radio-group v-model="n ...
- Django中执行原生SQL语句【新编辑】
参考我的个人博客 这部分迁移到了个人博客中:Django中执行原生SQL语句 这里需要补充一下,还有一个extra方法: ret = models.Student.objects.all().extr ...
- Linux awk 命令 说明
一. AWK 说明 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入.一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是l ...
- CSS-inline-block 间隙
间隙产生的原因是`inline-block`对外是`inline`,对内是`block`.`inline`会将连续的空白符解析为一个空格(如:下面示例的两个`li`之间的后面的换行空格).取消间隙的方 ...
- c++全局变量,局部变量,内存布局,默认初始化
全局变量 定义在所有函数之外的变量,main函数之内的变量也是局部变量,Globle variable 未显示初始化时执行默认初始化 局部变量 定义在函数之内的变量,Local variable 未 ...
- 11. Jmeter-后置处理器二
jmeter-后置处理器介绍与使用二 今天我们接着讲 JSR223 PostProcessor Debug PostProcessor JDBC PostProcessor Result Status ...