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. In the

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 <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
char s[];
int v1[],v2[],top;
int min_sum,ans;
int init(char *s,int *v) {
int top=;
for(int i=; s[i]; i++) {
while(s[i]==' ')
i++;
v[top]=;
while(s[i]&&isdigit(s[i])) {
v[top]=v[top]*+s[i]-'';
i++;
}
top++;
if(!s[i]) break;
}
return top;
}
int find(int *v,int n,int c) {
for(int i=n-; i>=; i--)
if(v[i]==c)
return i;
return ;
}
void build(int n,int *v1,int *v2,int sum) {
if(n<=)
return ;
int p=find(v1,n,v2[n-]);
//printf("v2[n-1]=%d p=%d\n",v2[n-1],p);
sum+=v2[n-];
if(p<=&&n-p-<=) {
if(sum==min_sum)
ans=min(ans,v2[n-]);
else if(sum<min_sum) {
min_sum=sum;
ans=v2[n-];
}
return ;
}
build(p,v1,v2,sum);
build(n-p-,v1+p+,v2+p,sum);
} int main() {
while(gets(s)) {
int v;
init(s,v1);
gets(s);
top=init(s,v2);
ans=min_sum=;
build(top,v1,v2,);
printf("%d\n",ans);
}
return ;
}

UVA548 Tree (二叉树的遍历)的更多相关文章

  1. 【日常学习】【二叉树遍历】Uva548 - Tree题解

    这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...

  2. [Leetcode] Binary tree level order traversal二叉树层次遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值

    [抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  4. Python与数据结构[3] -> 树/Tree[0] -> 二叉树及遍历二叉树的 Python 实现

    二叉树 / Binary Tree 二叉树是树结构的一种,但二叉树的每一个节点都最多只能有两个子节点. Binary Tree: 00 |_____ | | 00 00 |__ |__ | | | | ...

  5. 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

    [107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...

  6. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

  8. C++ 二叉树深度优先遍历和广度优先遍历

    二叉树的创建代码==>C++ 创建和遍历二叉树 深度优先遍历:是沿着树的深度遍历树的节点,尽可能深的搜索树的分支. //深度优先遍历二叉树void depthFirstSearch(Tree r ...

  9. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  10. [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

随机推荐

  1. Spring源码解析-配置文件的加载

    spring是一个很有名的java开源框架,作为一名javaer还是有必要了解spring的设计原理和机制,beans.core.context作为spring的三个核心组件.而三个组件中最重要的就是 ...

  2. Visaul Studio 常用快捷键动画演示

    从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...

  3. SICAU-OJ: A|B

    A|B 题意: 给出一个整数n(1<=n<=10100),求Σd(d满足d可以整除n),同时保证不存在x^2有x^2可以整除n. 另外,n的质因子满足小于等于1000. 题解: 这题是我第 ...

  4. hive Illegal Operation state transition from CLOSED to ERROR的处理

    异常堆栈如下: 2015-11-24 16:49:11,495 ERROR org.apache.hive.service.cli.operation.Operation: Error running ...

  5. AngularJs学习——何时应该使用Directive、Controller、Service?

    翻译:大漠穷秋 原文链接:http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ 一.简述 A ...

  6. Xcode5根控制器使用xib展示的步骤

    #error:Xcode5根控制器使用xib展示,步骤 ⓵取消mainInterface ⓶右击file's owner对xib进行view-view连线,否则: Terminating app du ...

  7. bzoj 2245 费用流

    比较裸 源点连人,每个人连自己的工作,工作连汇,然后因为人的费用是 分度的,且是随工作数非降的,所以我们拆边,源点连到每个人s+1条边 容量是每段的件数,费用是愤怒 /**************** ...

  8. Linux的yum命令——(八)

    Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...

  9. JSP页面中格式化日期为指顶格式

    有时候在页面中显示直接从数据库获取的日期时候会出现英文的日期格式.比如:

  10. python 实现定时循环触发某个方法

    直接贴上代码 import threading def sayhello(): print "hello world" global t #Notice: use global v ...