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. flask 开发环境搭建

    window下: 1)安装python 2)安装pip 3)使用pip install flask 如果成功安装使用pip list 既可以查看到flask的版本 ubuntu下的环境搭建 同样地使用 ...

  2. C++游戏界面不流畅的问题

    或许是我游戏玩多了,我突然发现,我的C++程序画面画面一顿一顿的,不流畅.肯定哪里不正确,要改. 奇怪啊,为什么我曾经,在我电脑上就不这么卡,就看不出画面一顿一顿的呢? 百度了,狗狗了,必应了,然而, ...

  3. 怎么在windows7系统我的电脑中添加快捷方式

    在我的电脑中添加一些快捷方式,这样不用每次在开始菜单中去找了 2 选择开始菜单运行 3 输入:Regedit命令 4 进入路径地址:HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  4. 全文检索引擎[asp版]

    search.asp: <% set DM=server.CreateObject("DeepMap.HLL")pnn=0: wdd="": pnn=Re ...

  5. python字符串转日期

    需要两步 为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成datetime类型 1.字符串与time类型的转换 >>> import time>> ...

  6. 验收测试 - WebDriver 5

    验收测试 - WebDriver - 配置 什么是WebDriver 这样说好了,它翻译起来就是Web驱动,用我的经验来说,它就是驱动浏览器运行的一个驱动器 有什么作用? 就像一个司机可以驱动一台汽车 ...

  7. iOS小技巧 - 如何生成范围随机数

    生成[0, N-1]的随机数 NSUInteger r = arc4random_uniform(N); 生成[1, N]的随机数 NSUInteger r = arc4random_uniform( ...

  8. 正则表达式表示 ja.resx 所在行

    [^\n]*ja.resx[^\n]*\n?正则表达式表示 ja.resx 所在行 用ultraEdit 删除关键字所在行的下一行或是上一行,所在行保留 删除 关键字所在行 的前3行: (^.*?(\ ...

  9. JAVA加解密 -- 数字签名算法

    数字签名 – 带有密钥的消息摘要算法 作用:验证数据完整性.认证数据来源.抗否认(OSI参考模型) 私钥签名,公钥验证 RSA 包含非对称算法和数字签名算法 实现代码: //1.初始化密钥 KeyPa ...

  10. linux 压缩、解压缩及归档工具

    linux下主要的压缩.归档工具 compress/uncompress: .Z gzip/gunzip:  .gz bzip2/bunzip2: .bz2 xz/unxz: .xz zip/unzi ...