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. 解决Post提交乱码问题

    在web.xml里面配置 <filter> <filter-name>charac</filter-name> <filter-class>org.sp ...

  2. 如何移除inline-block元素之间的空白

    我们想要的是<li>元素可以紧贴在一起,但是很显然,结果“出乎意料”.那么有什么方法可以让结果符合我们的预期呢?所能想到的解决方法至少有以下四种,而每种方法也都有其优劣所在,至于要如何选择 ...

  3. iOS 常用的#define合集

    1.定义常量 定义常量的时候最好以小写字母k开头,让人见名知意, (1)导航栏高度:我们都知道iPhone竖屏时候导航栏的高度为44,这时候可以定义一个常量来表示该高度, #define kNaivg ...

  4. 快速打开IIS的方法

    方法一: 在运行(win+r)输入inetmgr 方法二: 控制面板\所有控制面板项\管理工具\IIS 建议使用第一种方法

  5. [转载]Oracle Merge的使用

    FROM: http://zhangqchang.blog.163.com/blog/static/464989732009219114653226/ 摘至网上的几个例子 一.************ ...

  6. 蓝点通用管理系统V13版发布了!

    蓝点通用管理系统13版已发布! 重磅新功能:系统的通知和提醒功能,增加微信方式,微信通知.微信查询数据.微信拍照上传....... 蓝点的客户管理系统.进销存管理系统.产品管理系统.工作流管理系统.投 ...

  7. (转)spring boot实战(第三篇)事件监听源码分析

    原文:http://blog.csdn.net/liaokailin/article/details/48194777 监听源码分析 首先是我们自定义的main方法: package com.lkl. ...

  8. MPTCP 源码分析(六) 数据重发

    简述      TCP使用定时器函数tcp_retransmit_timer进行数据重发,MPTCP需要重发数据的时候, 不仅仅在原路径发送数据,而且会在另外一条子路径进行重发.这样考虑的原因是: 考 ...

  9. jQuery-DesktopGrid

    jQueryDesktopGrid jQueryDesktopGrid migrate to https://github.com/jelly-liu/jquery-osx jQuery deskto ...

  10. 个人或者企业怎么进行app开发?开发一款APP应用大概须要多少钱?

    App开发.是指专注于手机应用软件开发与服务. App是application的缩写,通常专指手机上的应用软件,或称手机client.另外眼下有非常多在线app开发平台.当然移动互联网时代是全民的移动 ...