递归求解,

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. $(document).ready 与 window.onload的区别?

    $(document).ready  = function(){}; DOM树加载完成时执行,此时文件不一定都已加载完成. window.onload = function(){}; DOM树加载完成 ...

  2. Linux文件属性相关补充及软硬连接

    第1章 文件属性相关 1.1 文件的属性 1.1.1 扩展名 windows  通过扩展名区分不同的类型的文件 linux 扩展名是给人类看的 方便我们区分不同类型文件 .conf      配置文件 ...

  3. 入门activiti-------1简单运行

    1.下载原料 2.放置位置 3.运行 4.成功页面和测试数据

  4. 普通平衡树代码。。。Treap

    应一些人之邀...发一篇代码 #include <iostream> #include <cstdio> #include <cstdlib> #include & ...

  5. ACM_Repeating Characters

    Repeating Characters Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...

  6. Unicode ,UTF-8,assic, gbk, latin1编码 的区别

    1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...

  7. maven 纯注解一步一步搭建Spring Mvc项目(入门)

    初次接触spring MVC项目,通过一段时间的学习,本文介绍一种以纯注解的方法去配置spring MVC环境,让那些配置的.xml文件统统见鬼吧. 什么是Spring MVC Spring MVC属 ...

  8. ios9-NSLayoutAnchor和UILayoutGuide实现自动布局

    @interface ViewController () { NSLayoutConstraint *yellowViewTopConstraint; NSLayoutConstraint *blue ...

  9. css3通过scale()实现放大功能、通过rotate()实现旋转功能

    css3通过scale()实现放大功能.通过rotate()实现旋转功能,下面有个示例,大家可以参考下 通过scale()实现放大功能 通过rotate()实现旋转功能 而transition则可设置 ...

  10. [Android]异常2-Unexpected error while executing

    异常原因: 可能一>Android Studio的自动编译没有成功 解决方法有: 解决一>菜单栏里的“Build”,“Clean Project” 注: