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 (建树+前序后序)的更多相关文章

  1. 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 ...

  2. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

  3. UVA 548 Tree 建树

    题意: 输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小. 思路: 输入后建树,然后dfs求最小的叶子. #include<iostream> #include<cstdi ...

  4. UVa 536 Tree Recovery(二叉树后序遍历)

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...

  5. DS Tree 已知后序、中序 => 建树 => 求先序

    注意点: 和上一篇的DS Tree 已知先序.中序 => 建树 => 求后序差不多,注意的地方是在aftorder中找根节点的时候,是从右往左找,因此递归的时候注意参数,最好是拿纸和笔模拟 ...

  6. Tree Recovery(前序中序求后序)

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14640   Accepted: 9091 De ...

  7. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  8. UVa 548 Tree(中序遍历+后序遍历)

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

  9. DS Tree 已知先序、中序 => 建树 => 求后序

    参考:二叉树--前序和中序得到后序 思路历程: 在最初敲的时候,经常会弄混preorder和midorder的元素位置.大体的思路就是在preorder中找到根节点(根节点在序列的左边),然后在mid ...

随机推荐

  1. 回炉Spring--Bean生命周期及AOP

    Spring容器: 在基于Spring的应用中,你的应用对象生存于Spring容器(container)中,Spring容器负责创建对象,装配它们,配置它们并管理它们的整个生命周期,从生存到死亡.(在 ...

  2. 圆周率Pi是如何计算出来的

    object SparkPi { def main(args: Array[String]) { val spark = SparkSession .builder .appName("Sp ...

  3. Python实现BFS,DFS

    BFS:队 graph = { "A" : ["B","C"], "B" : ["A"," ...

  4. 12. I2C-EEPROM

    12.1. I2C 协议简介 I 2 C ( Inter-Integrated Circuit )协议是由 Phiilps 公司开发的,由于它具引脚少,硬件实现简单,可扩展性强,不需要如 USART. ...

  5. Oracle下定时删除归档日志脚本

    一.报错信息 前几天网站突然访问不了,并且报了如下错误: ora-27101: shared memory realm does not exist ora-01034: oracle not ava ...

  6. Java学习之包

    一.包:就是类的命名空间(在文件系统中的表现形式就是文件夹) 二.代码编写规则 1.写在程序文件的第一行 2.格式:package 包名[.包名1.包名2......] 类的全名称 包名.类名 例如: ...

  7. 哪位有方法把 dd/mm/yyyy的字符串 格式化成yyyy-mm-dd

     哪位有方法把  dd/mm/yyyy的字符串 格式化成yyyy-mm-dd[总监]Dawood(656317124)  10:00:42啊,找到方法了.procedure TForm1.Button ...

  8. 关于日历实现代码里lunarInfo(农历)数组

    var lunarInfo=new Array( 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x0 ...

  9. LOJ6682 梦中的数论

    题目 不难发现我们要求的东西是\(\sum_{i=1}^n\binom{\sigma(i)}{2}=\sum_{i=1}^n\frac{\sigma(i)(\sigma(i)-1)}{2}=\frac ...

  10. python3 tkinter模块小项目联系之邮箱客户端

    # -*- coding:utf-8 -*- from tkinter import * from tkinter.messagebox import askyesno, showerror, sho ...