递归求解,

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, 从中序和后续回复二叉树)的更多相关文章

  1. 洛谷:P1087 FBI树 P1030 求先序排列 P1305 新二叉树

    至于为啥把这三个题放到一起,大概是因为洛谷的试炼场吧,三道树的水题,首先要理解 先序中序后序遍历方法. fbi树由于数量小,在递归每个区间时,暴力跑一遍区间里的数,看看是否有0和1.至于递归的方法,二 ...

  2. LeetCode106. 从中序与后序遍历序列构造二叉树

    106. 从中序与后序遍历序列构造二叉树 描述 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例 例如,给出 中序遍历 inorder = [9,3,15,20 ...

  3. [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 ...

  4. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  5. LeetCode(106):从中序与后序遍历序列构造二叉树

    Medium! 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 posto ...

  6. [LeetCode系列] 从中序遍历和后序遍历序列构造二叉树(迭代解法)

    给定中序遍历inorder和后序遍历postorder, 请构造出二叉树. 算法思路: 设后序遍历为po, 中序遍历为io. 首先取出po的最后一个节点作为根节点, 同时将这个节点入stn栈; 随后比 ...

  7. [leetcode]从中序与后序/前序遍历序列构造二叉树

    从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 po ...

  8. Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树

    Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...

  9. Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树

    106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...

随机推荐

  1. phpci发送邮件

    $config['protocol']='smtp'; $config['smtp_host']='smtp.163.com';//163服务器,之前用了qq服务器死活发不出去,不知道什么原因,可以自 ...

  2. bzoj1877 晨跑(费用流)

    1877: [SDOI2009]晨跑 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 2138  Solved: 1145 Description Elax ...

  3. 【Codeforces1117C_CF1117C】Magic Ship(构造)

    题目: Codeforces1117C 考的时候很困,开局半小时后才过A,只做出来AB,排名3000+,掉了119--半夜体验极差. 翻译: 你是一个船长.最初你在点 \((x_1,y_1)\) (显 ...

  4. ACM_圆的面积

    圆的面积 Time Limit: 2000/1000ms (Java/Others) Problem Description: AB是圆O的一条直径,CD.EF是两条垂直于AB的弦,并且以CD为直径的 ...

  5. JdbcTemplate:Jdbc模板和数据库元数据

    通过 Jdbc .C3P0 .Druid 的使用我们会发现即使我们做了工具的封装,但重复性的代码依旧很多.我们可以通过 JdbcTemplate 即 Jdbc 模板来使我们的代码更加简洁,逻辑更加清晰 ...

  6. Sql Server 如何解决多并发情况下,出现的多个相同ID数据

    在数据库中单独创建一张表,保存当前存储状态,“存储过程”  设置访问条件root初始值为“0” 如果root值不为0的时候就不可访问并进行相关操作. 在事务执行前将root值设置为1,事务结束后将ro ...

  7. hibernate annotation 之 一对多、多对一双向外键关联

    假设,一个农场产出多种植物,具体的某一植物产于某一农场. 3 import java.io.Serializable; 4 import java.util.Set; 5 import javax.p ...

  8. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  9. asp.net——根据时间,显示内容

    题目: 在VS 2010中建立一个网站,命名为Lab5_1,建立时注意项目文件夹的存放位置.根据当前时间,在页面上显示早上好或下午好或晚上好,并显示相应的不同图片. 体验: 一开始看到这个题目的时候, ...

  10. AIDL跨进程通信报Intent must be explicit

    在Android5.0机子上采用隐式启动来调试AIDL时,会出现Intent must be explicit的错误,原因是5.0的机子不允许使用隐式启动方式,解决的方法是:在启动intent时添加i ...