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
1
3
255
/**
题目:Tree UVA - 548
链接:https://vjudge.net/problem/UVA-548
题意:算法竞赛入门经典P155 eg6-8
思路:后序遍历的最后一个为根。那么知道了根是多少,就可以在中序遍历找到根的位置,
根的左边为左子树,右边为右子树。左子树,右子树的后序遍历也可以通过原来的后序遍历中分成两部分获得。
后序遍历和中序遍历长度相同。
递归处理每一个结点即可。 收获:
char s[];
stringstream ss(s);
n = 0;
int x;
while(ss>>x) a[n++] = x; 处理一行由数字和空格组成的字符串,划分成数字的方式。
*/ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e4+;
const int mod = 1e9+;
int a[maxn], b[maxn], n;
struct node
{
int value;
node *left, *right;
node():left(NULL),right(NULL){}
};
node *root;
char s[*maxn];
void Input(char *s,int a[])
{
stringstream ss(s);
n = ;
int x;
while(ss>>x) a[n++] = x;
}
node* build(int l,int r,int p)
{
if(l>r){
return NULL;
}
node *x = new node();
int mid;
for(int i = l; i <= r; i++){
if(a[i]==b[p]){
mid = i; break;
}
}
x->value = a[mid];
x->left = build(l,mid-,p--(r-mid));///可推算出根的位置。
x->right = build(mid+,r,p-);
return x;
}
int ans, leaf;
void Find(node *root,int sum)
{
if(root->left==NULL&&root->right==NULL){
if(sum+root->value<ans){
ans = sum + root->value;
leaf = root->value;
}else
{
if(sum+root->value==ans&&root->value<leaf){
leaf = root->value;
}
}
}else
{
if(root->left!=NULL){
Find(root->left,sum+root->value);
}
if(root->right!=NULL){
Find(root->right,sum+root->value);
}
}
}
int main()
{
while(gets(s)!=NULL){
Input(s,a);
gets(s);
Input(s,b);
root = build(,n-,n-);///中序遍历[0,n-1],第三个n-1表示根。
ans = maxn*maxn;
Find(root,);
printf("%d\n",leaf);
}
return ;
}

Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。的更多相关文章

  1. TZOJ 3209 后序遍历(已知中序前序求后序)

    描述 在数据结构中,遍历是二叉树最重要的操作之一.所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问. 这里给出三种遍历算法. 1.中序遍历的递归算法定义:  ...

  2. PAT1020 (已知中序,后序遍历转前序遍历)

    已知后序与中序输出前序(先序):后序:3, 4, 2, 6, 5, 1(左右根)中序:3, 2, 4, 1, 6, 5(左根右) 已知一棵二叉树,输出前,中,后时我们采用递归的方式.同样也应该利用递归 ...

  3. UVA - 548 根据中序遍历和后序遍历建二叉树(关于三种遍历二叉树)

    题意: 同时给两个序列,分别是二叉树的中序遍历和后序遍历,求出根节点到叶子结点路径上的权值最小和 的那个 叶子节点的值,若有多个最小权值,则输出最小叶子结点的和. 想法: 一开始想着建树,但是没有这样 ...

  4. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  5. Tree UVA - 548(二叉树递归遍历)

    题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...

  6. 二叉树:前序遍历、中序遍历、后序遍历,BFS,DFS

    1.定义 一棵二叉树由根结点.左子树和右子树三部分组成,若规定 D.L.R 分别代表遍历根结点.遍历左子树.遍历右子树,则二叉树的遍历方式有 6 种:DLR.DRL.LDR.LRD.RDL.RLD.由 ...

  7. lintcode: 中序遍历和后序遍历树构造二叉树

    题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1    3 注意 你可 ...

  8. 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)

    好不容易又到周五了,周末终于可以休息休息了.写这一篇随笔只是心血来潮,下午问了一位朋友PAT考的如何,顺便看一下他考的试题,里面有最后一道题,是关于给出中序遍历和后序遍历然后求一个层次遍历.等等,我找 ...

  9. LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树

    中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...

随机推荐

  1. iOS计算富文本(NSMutableAttributedString)高度

    有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ...

  2. 今天升级Xcode 7.0 bata发现网络访问失败。

    今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Securit ...

  3. 【ArcGIS 10.2新特性】ArcGIS 10.2将PostgreSQL原生数据发布为要素服务

    1.ArcGIS 10.2支持原生数据发布为要素服 有没有将自己已有的空间数据发布为要素服务的需求?有没有将非Esri空间数据类型的数据作为服务在Web端展示的需求?     ArcGIS 10.2 ...

  4. svn的安装(整合apache、ldap)包括错误解决post commit FS processing had error

    2013年12月5日 admin 发表评论 阅读评论 以下是centos环境下,以yum安装apache及其相关软件.svn使用源码包编译,使用官网最新的1.8.5版本. 一.安装apache ope ...

  5. 【spring boot】4.spring boot配置多环境资源文件

    一个spring boot 项目在开发环境.测试环境.生产环境下,好多的配置都是不尽相同的.所以配置多分的资源文件,仅仅在部署在不同环境的时候,选择激活不同的资源文件就可以实现多环境的部署. 项目结构 ...

  6. 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代

    package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...

  7. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  8. PHP中单引号双引号使用原则

    PHP中单引号双引号使用原则   1.PHP中尽量用单引号,HTML代码全部用双引号   2.在包含变量的时候,用双引号可以简化操作   3.复杂的情况下用大括号包起来   4 PHP引号还有一个用处 ...

  9. HDU - 3038 How Many Answers Are Wrong (带权并查集)

    题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用.[a, b]和为s,所以a-1与b就能够确定一次关系.通过计算与根的距离能够推断出询问的正确性 #inclu ...

  10. DB2日期与时间

    摘选自:http://www.cnblogs.com/wanghonghu/archive/2012/05/25/2518604.html 1.db2可以通过SYSIBM.SYSDUMMY1.SYSI ...