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 ...
随机推荐
- [CF475E]Strongly Connected City 2
题目大意:给一张$n(n\leqslant2000)$个点的无向图,给所有边定向,使定向之后存在最多的有序点对$(a,b)$满足从$a$能到$b$ 题解:先把边双缩点,因为这里面的点一定两两可达. 根 ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【NOIP模拟赛】Drink 二维链表+模拟
我觉得这道题的主旨应该是模拟,但是如果说他是二维链表的話也不為過.這道題的主體思路就是把原來旋轉點的O(n^2)變成了旋轉邊界的O(n).怎麼旋轉邊界呢,就好像是把原來的那些點都於上下左右四個點連線, ...
- 【COGS 461】[网络流24题] 餐巾 最小费用最大流
既然是最小费用最大流我们就用最大流来限制其一定能把每天跑满,那么把每个表示天的点向T连流量为其所需餐巾,费用为0的边,然后又与每天的餐巾对于买是无限制的因此从S向每个表示天的点连流量为INF,费用为一 ...
- 【可持久化线段树?!】rope史上最全详解
https://www.luogu.org/problemnew/show/P3919 看到上面链接中的题时,我在学会可持久化线段树的同时,第一次学会了一个非常屌(cai)的STL大法——rope!! ...
- [洛谷P1541] 乌龟棋
洛谷题目链接:乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩 ...
- Python基础(4)_集合、布尔类型
一.集合 集合的作用一:关系运算集合的作用二:去重 定义集合:集合内的元素必须是唯一的:集合内的元素必须是可hash的,也是就不可变类型:集合是无序的 s={'egon',123,'egon','1' ...
- 【Git】GitHub之多人开发一个项目
首先我们要简单知道github跟Git的区别.git是版本控制工具, github是一个面向开源及私有软件项目的托管平台,也是程序员交流的地方. 接下来就开始讲怎么多人一起开发. 首先我们先拥有git ...
- 使用QML创建第一个界面(转)
原文转自 https://blog.csdn.net/rl529014/article/details/51378307 在Qt编程中,我们可以使用纯C++代码,或C++和XML结合的方式来创建GUI ...
- Newtonsoft.Json 序列化和反序列化 以及时间格式 2
一.JSON使用JsonPropertyAttribute重命名属性名 1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称.注意:属性Name和Directo ...