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. 使用gettext提取c#中的多语言占位符(nopCommerce示例篇)

    i18n国际化通常的作法是使用gettext,即在源码中使用特殊的关键字来标识这个字符串将可能被翻译,如 @if (Model.IsCustomerForumModerator) { <li c ...

  2. [PATCH] ARM: add dtbImage.<dt> and dtbuImage.<dt> rules

    转载: http://permalink.gmane.org/gmane.linux.kbuild.devel/8755 This rules are useful for appended devi ...

  3. 日积月累--exception记录

    关于Android的sqlite数据类型text长度限制的问题? 这也许不能称为一个bug,但是比较坑,所以贴在了这里.在Android的sqlite中存储一个字符串,发现总是数据丢失,我去查询sql ...

  4. Kubernetes Fluentd+Elasticsearch+Kibana统一日志管理平台搭建的填坑指南

    在初步完成Kubernetes集群架构的建立后,通过搭建一些监控组件,我们已经能够实现 图形化的监控每个node,pod的状态信息和资源情况 通过scale进行replicateSet的扩展和伸缩 通 ...

  5. 本机搭建PHP环境全教程(图文)

    为了更好的维护空间网站,研究和调试PHP程序,许多人需要在自己的计算机内搭建PHP环境.本文将介绍使用phpnow环境组件搭建的全过程.使用搜索工具,搜索phpnow<ignore_js_op& ...

  6. 安装office2016 64位时提示64位与32位的office程序不兼容,在系统是64位的情况下,由于应用的需要,必须装64位的office,怎么办

    解决办法如下: 如果是,那就看看32位的能不能安装了,要是能,就重新安装一次,把所有组件全部安装,然后,在进行卸载,一般可以卸载成功 如果卸载不成功,这个时候再使用微软的专用卸载工具,——要认清,一定 ...

  7. Linux下免安装mysql

    我是使用免安装的包mysql-5.6.30-linux2.6-x86_64.tar.gz(在http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6 ...

  8. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-报错0X4655,18005错误怎么办

    首先确认驱动器没有报错(如果驱动器报错,请先解决绝对值编码器的清除多圈数据问题) 报错一般上使能就会报错,没法测试运转,而且不管是用贝福自带的NC功能还是自己写的都会一样的效果   请删除在贝福的Et ...

  9. react-native-storage + AsyncStorage 实现数据存储

    1.组件封装 import Storage from 'react-native-storage'; import { AsyncStorage } from 'react-native'; cons ...

  10. HDU 4632 Palindrome subsequence(区间dp)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...