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. Swift开发经验——外部参数名

    一.什么是外部参数名? 浅显地说,外部参数名就是在调用一个方法时要在方法的参数前面加上一个特定的名字,目的是便于阅读代码,提高维护效率.   二.在最新的Xcode中,外部参数名的性质与用法如下 性质 ...

  2. Jetty错误:java.lang.IllegalStateException: Form too large 270468>200000的问题解决

    说明: 1.200000单位为byte,并不是2MB,而是200KB,换算参考:https://calc.itzmx.com/ 2.这个是表单提交后长度超过了200KB造成的,除了表单Form,还有U ...

  3. Mongodb 学习笔记简介

    目录 1       准备工作... 5 1.1        相关网址... 6 1.1        下载安装... 6 1.1.1         下载:... 6 1.1.2         ...

  4. 【记录一下】phpMyAdmin 4.5.0-beta1 发布,要求 PHP 5.5

    详情点击: [开源中国]http://www.oschina.net/news/65696/phpmyadmin-4-5-0-beta1 [phpMyAdmin]https://www.phpmyad ...

  5. LiDAR Textbook & Automated Road Network Extraction

    Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...

  6. TDiocpCoderTcpServer返回数据记录有条数限制的问题

    TDiocpCoderTcpServer返回数据记录有条数限制的问题 在使用TDiocpCoderTcpServer控件返回查询数据的时候,发现当记录条数超过一定数量的时候(比方有人反试图返回30万条 ...

  7. 【docker】查看docker镜像的版本号TAG,从远程仓库拉取自己想要版本的镜像

    要想查看镜像的版本好TAG,需要在docker hub查看 地址如下:https://hub.docker.com/r/library/ 进入之后,在页面左上角搜索框搜索, 例如搜索redis 搜索完 ...

  8. Microsoft-PetSop4.0(宠物商店)-数据库设计-Sql

    ylbtech-DatabaseDesgin:Microsoft-PetSop4.0(宠物商店)-数据库设计-Sql DatabaseName:PetShop(宠物商店) Model:宠物商店网站 T ...

  9. angular学习(二)—— Data Binding

    转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...

  10. 在线安装eclipse中html/jsp/xml editor插件 eclipseeditor

    1.打开eclipse中的help————>Install New Software 2.点击Add按钮,然后弹出一个框,第一个文本框可以随便写,第二个一定要写: http://download ...