UVA548 Tree (二叉树的遍历)
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 (二叉树的遍历)的更多相关文章
- 【日常学习】【二叉树遍历】Uva548 - Tree题解
这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...
- [Leetcode] Binary tree level order traversal二叉树层次遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 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 ...
- Python与数据结构[3] -> 树/Tree[0] -> 二叉树及遍历二叉树的 Python 实现
二叉树 / Binary Tree 二叉树是树结构的一种,但二叉树的每一个节点都最多只能有两个子节点. Binary Tree: 00 |_____ | | 00 00 |__ |__ | | | | ...
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...
- [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 ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- C++ 二叉树深度优先遍历和广度优先遍历
二叉树的创建代码==>C++ 创建和遍历二叉树 深度优先遍历:是沿着树的深度遍历树的节点,尽可能深的搜索树的分支. //深度优先遍历二叉树void depthFirstSearch(Tree r ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- [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 ...
随机推荐
- Codeforces Round #532 (Div. 2) 题解
Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...
- 牛客 国庆七天乐 day1 L
https://www.nowcoder.com/acm/contest/201/L 题意:给你两条平行的直线和n个圆,在直线上面行走和在圆上和在圆内行走不需要耗费体力,除了这些区域外平面上经过任意两 ...
- mysql之蠕虫复制
蠕虫复制 蠕虫复制:从已有的数据表中获取数据,然后将数据进行新增操作,数据成倍(以指数形式)的增加. 根据已有表创建新表,即复制表结构,其基本语法为: create table + 表名 + like ...
- sqrti128
求平方根下取整,对于gcc type __uint128_t. ~45.5ns/op on i7-7700k@4.35G,即typical <200cyc/op. Together with u ...
- 深入探索 高效的Java异常处理框架
转载自:http://www.sunwei.org/archives/196 摘要:本文从Java异常最基本的概念.语法开始讲述了Java异常处理的基本知识,分析了Java异常体系结构,对比Sprin ...
- saltstack入门至放弃之salt安装部署
学习了一段时间的saltstack,是时候记录下了.友提:学习环境是两台centos_7.2_x64机器 系统初始化: 两台机器执行以下脚本即可(友提:两台服务器的主机名配置在/etc/hosts中, ...
- TCP(一)
TCP的特点:三次握手.四次挥手.可靠连接.丢包重传.所有的关键词都围绕着可靠传输. 实现可靠传输的核心机制:seq+ack.通过ack判断是否有丢包,是否需要重传. 三次握手 1)初始状态:clie ...
- Sass、Ruby、Nodejs、gulp
1.Sass文件就是普通的文本文件,不过其文件后缀名有两种,一种为“.sass”:另一种为“.scss”.我们一般用“.scss”就好,至于这两种文件扩展名的区别在于“.sass”是Sass语言文件的 ...
- SSL_TLS
http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html 配置 IBM HTTP Server 以支持 TLS 1.2 http://www-01.ibm ...
- autoKeras入门
测试本地mnist数据集 图片只用500张,450张做train与50张test, 代码如下: # conding:utf-8 import os os.environ[' import numpy ...