递归求解,

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. NOI2015 软件包管理器(树链剖分+线段树)

    P2146 软件包管理器 题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决 ...

  2. ACM_小凯的排序(字符串)

    小凯的排序 Time Limit: 2000/1000ms (Java/Others) Problem Description: 调皮的小凯喜欢排序,拿到什么东西都要排一下序.现在他觉得单一的递增递减 ...

  3. [转]HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery

    本文转自:http://pietschsoft.com/post/2010/11/17/HTML5-Day-4-Add-DropDown-Menu-ASPNET-MVC-HTML5-Template- ...

  4. 跨域请求之jsonp

    1.什么是跨域请求: 服务器A上的一个页面,要请求服务器B上的一个处理程序,这就叫做跨域请求 本次的测试页面为: 处理程序kimhandler.ashx,如下: http://qxw119243026 ...

  5. Android 6.0一个完整的native service

     上一篇博客<Android 6.0 如何添加完整的系统服务(app-framework-kernel)>http://www.cnblogs.com/hackfun/p/7418902. ...

  6. C#入门经典 Chapter1&2

    Chapter1 1.1 .Net Framework的内容 主要包含一个庞大的代码库,可以在客户端通过OOP来使用这些代码(OOP:Object Oriented Programming面对对象程序 ...

  7. ASP.net参数传递总结

    同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...

  8. jdbc 使用谨记

    jdbc是java操作数据库的杀手锏.所有java程序员,对jdbc应该都不陌生. 但是,应该你也曾经被其折磨的抓耳挠腮,咬牙切齿吧,也许正因为这样你才对其记忆犹新,刻骨铭心. 这里有一些使用jdbc ...

  9. Python语言之控制流(if...elif...else,while,for,break,continue)

    1.if...elif...else... number = 23 guess = int(input('Enter an integer : ')) if guess == number: prin ...

  10. HDU_1068_Girls and Boys_二分图匹配

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...