树 (p155, 从中序和后续回复二叉树)
递归求解,
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.
Inthe 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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 10003
#define INF 1000000009
int a[MAXN], b[MAXN];//中序遍历 和 后序遍历
int ans,k;
struct node
{
int left, right;
}T[MAXN];
int build(int inbeg, int inend, int pbeg, int pend)
{
if (inbeg > inend || pbeg > pend)
return -;
int r = b[pend];
int p = inbeg,cnt = ;
while (a[p] != r)
p++;
cnt = p - inbeg;
T[r].left = build(inbeg, p - , pbeg, pbeg + cnt-);
T[r].right = build(p + , inend, pbeg + cnt, pend - );
return r;
}
void get_ans(int x, int sum)
{
sum += x;
if (T[x].left == - && T[x].right == -)
{
if (sum < ans || (sum == ans&&x < k))
{
ans = sum;
k = x;
}
}
if (T[x].left != -) get_ans(T[x].left, sum);
if (T[x].right != -) get_ans(T[x].right, sum);
}
int main()
{
string tmp;
while (getline(cin, tmp))
{
stringstream s(tmp);
int i = , j = ;
while (s >> a[i]) i++;
getline(cin, tmp);
stringstream s1(tmp);
for (j = ; j < i; j++)
{
s1 >> b[j];
}
int n = i, root = b[i - ];
ans = INF;
for (int i = ; i < MAXN; i++)
T[i].left = T[i].right = ;
build(, n - , , n - );
get_ans(root, );
cout << k << endl;
}
}
把求解和递归合并
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 10003
#define INF 1000000009
int a[MAXN], b[MAXN];//中序遍历 和 后序遍历
int ans,k;
struct node
{
int left, right;
}T[MAXN];
int build(int inbeg, int inend, int pbeg, int pend,int sum)
{
if (inbeg > inend || pbeg > pend)
return -;
int r = b[pend];
sum += r;
int p = inbeg,cnt = ;
while (a[p] != r)
p++;
cnt = p - inbeg;
T[r].left = build(inbeg, p - , pbeg, pbeg + cnt-,sum);
T[r].right = build(p + , inend, pbeg + cnt, pend - ,sum);
if (T[r].left == - && T[r].right == -)
{
if (sum < ans || (sum == ans&&r < k))
{
ans = sum;
k = r;
}
}
return r;
}
/*
void get_ans(int x, int sum)
{
sum += x;
if (T[x].left == -1 && T[x].right == -1)
{
if (sum < ans || (sum == ans&&x < k))
{
ans = sum;
k = x;
}
}
if (T[x].left != -1) get_ans(T[x].left, sum);
if (T[x].right != -1) get_ans(T[x].right, sum);
}
*/
int main()
{
string tmp;
while (getline(cin, tmp))
{
stringstream s(tmp);
int i = , j = ;
while (s >> a[i]) i++;
getline(cin, tmp);
stringstream s1(tmp);
for (j = ; j < i; j++)
{
s1 >> b[j];
}
int n = i, root = b[i - ];
ans = INF;
for (int i = ; i < MAXN; i++)
T[i].left = T[i].right = ;
build(, n - , , n - ,);
cout << k << endl;
}
}
树 (p155, 从中序和后续回复二叉树)的更多相关文章
- 洛谷:P1087 FBI树 P1030 求先序排列 P1305 新二叉树
至于为啥把这三个题放到一起,大概是因为洛谷的试炼场吧,三道树的水题,首先要理解 先序中序后序遍历方法. fbi树由于数量小,在递归每个区间时,暴力跑一遍区间里的数,看看是否有0和1.至于递归的方法,二 ...
- LeetCode106. 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树 描述 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例 例如,给出 中序遍历 inorder = [9,3,15,20 ...
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- LeetCode(106):从中序与后序遍历序列构造二叉树
Medium! 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 posto ...
- [LeetCode系列] 从中序遍历和后序遍历序列构造二叉树(迭代解法)
给定中序遍历inorder和后序遍历postorder, 请构造出二叉树. 算法思路: 设后序遍历为po, 中序遍历为io. 首先取出po的最后一个节点作为根节点, 同时将这个节点入stn栈; 随后比 ...
- [leetcode]从中序与后序/前序遍历序列构造二叉树
从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 po ...
- Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树
Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...
- Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...
随机推荐
- [Swift通天遁地]九、拔剑吧-(15)搭建具有滑出、视差、3D变形等切换效果的引导页
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- keystone身份认证服务
Keystone介绍 keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户权限的定义等等.云 ...
- Python细节(一)深浅拷贝
深浅拷贝 只要涉及拷贝,就会涉及创建新对象 浅拷贝,会创建一个新的容器,列表中的元素和原列表的元素用的是同一个内存空间 第一种方法:从头切到尾,完整的复制一份 lst = [1,2,3,4] lst1 ...
- jmeter中beanshell断言的使用
简单使用beanshell的内容,进行测试内容的判断 这里通过断言内容,修改if的条件,达到发送警报邮件的功能 beanshell 代码如下: SampleResult 等效于 prev lo ...
- Maven+Docker,发布到Registry
1.配置Pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnc ...
- [转]使用ThinkPHP框架快速开发网站(多图)
本文转自:http://blog.csdn.net/ruby97/article/details/7574851 这一周一直忙于做实验室的网站,基本功能算是完成了.比较有收获的是大概了解了ThinkP ...
- centos安装composer以及使用国内镜像
下载composer.phar文件 curl -sS https://getcomposer.org/installer | php 将composer.phar移动到环境变量中并且更名为compos ...
- Spring Cloud (6) 分布式配置中心-高可用
高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...
- VS插件-Resharper
最近代码因为Resharper出现了点问题,同事问我这个插件有什么用,下面就列几个最近常用的功能.其他功能后续慢慢更新 1.什么是Resharper ReSharper是一个JetBrains公司出品 ...
- 【1】Jdk1.8中的HashMap实现原理
HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 内部实现 ...