树 (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] 后序 ...
随机推荐
- 0606-工厂模式、单例模式、DBDA的单例和完整功能
工厂模式:只要指定类名,就可以据此获取一个该类的对象. 单例模式:某个类,只允许其“创建”出一个对象. 单例的方法:三私一公(一个私有化对象,一个私有化构造方法,一个私有化克隆方法,一个公共方法返回对 ...
- LDA PCA 学习笔记
提要: 本文主要介绍了和推导了LDA和PCA,参考了这篇博客 LDA LDA的原理是,将带上标签的数据(点),通过投影的方法,投影到维度更低的空间中,使得投影后的点,会形成按类别区分,一簇一簇的情况, ...
- day03_12/13/2016_bean的管理之依赖注入
- Android项目实战_手机安全卫士home界面
# 安全卫士主页面# ###1.GridView控件 1.与ListView的使用方式差不多,也要使用数据适配器,通过设置android:numColumns控制显示几列 2.通过指定android: ...
- Android_撕衣服小案例
一直都觉得做安卓开发挺有意思,最近一段时间都在学习这方面的知识以及练习敲代码这次要说的是一个简单有趣的案例,相信大家也是看了标题才进来的吧,是不是有点迫不及待的想看看效果图,嘿嘿,算了还是直接给上源码 ...
- js 翻页
翻页功能是js很基础的一个算法,且用得很多,所以必须掌握此项技能. 我们要想清楚在实现翻页的过程中需要哪几个步骤: 1.我们首先需要的变量有哪些,必须的有一个存放当前页码的变量nowPage.一个存放 ...
- spring 将配置文件中的值注入 属性
1.编写配置文件 #债权转让 #默认周期 必须大于0 credit.defaultDuration=1 #最小转让金额(元) credit.minBidAmount=1.00 #最小转让时间 到期时间 ...
- java与javascript之间json格式数据互转
javascript中对象与字符串的互转 对象转为字符串:通过JSON.encode方法,这个是json.js里面的方法,引入到当前文件就可以了. 字符串转换为对象:①使用JSON.decode方法, ...
- 仿iphone动态萤火虫锁屏应用安卓源码
该源码是仿iphone动态萤火虫锁屏应用源码,源码SkyLock,这也是最近弄了一款锁屏,苦于市场百般阻拦与锁屏应用数量实在太多,于是将它拿出来开源:废话不多说,希望大家能够希望,更多说明请看下面的吧 ...
- C# 执行sql语句批量更新
int x = db.Database.ExecuteSqlCommand(string.Format("update T_Pension SET UnitType = '{0}' WHER ...