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. pyCharm和解释器下载安装

    参考:(mac) 安装流程和注意: http://blog.csdn.net/limin2928/article/details/69267184 解释器下载地址: https://www.pytho ...

  2. 二、制作BOM表格--物料表格--Bill of Materials

    二.制作BOM表格--物料表格--Bill of Materials 公司会根据这个表格进行相关元器件的采购--以及后期的贴片上彩 操作: .dsn--Tools--Bill of Materials ...

  3. 【Java】Java引用maven私服jar包及jar包提交私服问题

    pom.xml中加入以下配置即可 1.引用私服jar包 <!-- 加载的是 第三方项目使用的jar包 --> <repositories> <repository> ...

  4. 2018-2019-2 20175120 实验四《Android程序设计》实验报告

    任务一:Android Studio的安装测试 任务要求:参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: 参考 ...

  5. python排序算法-冒泡和快速排序,解答阿里面试题

    ''常见的排序算法\ 插入排序/希尔排序/直接排序/堆排序 冒泡排序/快速排序/归序排序/基数排序 给定一个列表,将这个列表进行排序,要求:> 时间复杂度要小于O(n^2) 复杂度:1.时间复杂 ...

  6. nmon使用及监控数据分析

    https://blog.csdn.net/sean4m/article/details/79892387

  7. putchar(".:-=+*#%@"[(int)(d * 5.0f)])

    前两天在玩知乎时候见到有个用C语言画心的小代码感觉还是蛮好玩的,不过,里面有行代码看了好久才懂: putchar(".:-=+*#%@"[(int)(d * 5.0f)]); ,先 ...

  8. 50.Maximal Square(最大正方形)

    Level   Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...

  9. mysql修改root密码和设置权限 转摘:http://www.cnblogs.com/wangs/p/3346767.html

    整理了以下四种在MySQL中修改root密码的方法,可能对大家有所帮助! 方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR ' ...

  10. C#设计模式:观察者模式(Observer Pattern)

    一,什么是观察者模式(Observer Pattern)? 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知它的依赖对象 二,代码 ...