UVA548-Tree(二叉树数组表示)
Problem UVA548-Tree
Accept: 2287 Submit: 13947
Time Limit: 3000 mSec
Problem Description
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
题解:这个题有两个有价值的地方,一个是二叉树的数组实现,一个是通过中序和后序遍历构造处二叉树的先序遍历。
二叉树的数组实现也是递归的方式,在这个地方由于题目中说明节点权值都不一样而且权值的范围也很适合作为数组下标,因此直接用权值作为节点下标建树。
通过中序和后序遍历构造先序遍历建树并不困难,只要熟悉三种遍历的过程,就很容易找到方法。后序遍历的最后一个必定是根节点,然后他的前面,一部分是左子树,一部分是右子树,
这个时候问题就是精确到哪一位是左子树,利用中序遍历轻松搞定,在中序遍历中找到根节点,左边就是左子树,右边就是右子树,那各有几个就很清楚了,之后就交给递归就好了,需要注意递归基,
也就是在中序遍历序列中找不到子树区间的时候,也就是左端点 > 右端点的时候。
一般情况下,用数组实现二叉树需要自己“申请”节点,也就是和链式前向星里的tot++差不多的操作
const int root = ;
int cnt; void newtree(){
lchild[root] = rchild[root] = ;
have_val[root] = false;
cnt = root;
} int newnode(){
int u = ++cnt;
lchild[u] = rchild[u] = ;
have_val[u] = false;
return u;
}
摘自紫书。
以下是该题代码
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int maxn = +;
int in_order[maxn],post_order[maxn];
int lchild[maxn],rchild[maxn];
int n,ans,Min; bool read_list(int *num){
string str;
if(!getline(cin,str)) return false;
stringstream ss(str);
n = ;
int x;
while(ss >> x){
num[n++] = x;
}
return n > ;
} int build(int l1,int r1,int l2,int r2){
if(l1 > r1) return ;
int root = post_order[r2];
int i = l1;
while(in_order[i] != root) i++;
int cnt = i-l1;
lchild[root] = build(l1,i-,l2,l2+cnt-);
rchild[root] = build(i+,r1,l2+cnt,r2-);
return root;
} void dfs(int root,int val){
val += root;
if(!lchild[root] && !rchild[root]){
if(Min == val) ans = ans < root ? ans : root;
else if(val < Min) ans = root,Min = val;
return;
}
if(val > Min) return;
if(lchild[root]) dfs(lchild[root],val);
if(rchild[root]) dfs(rchild[root],val);
} int main()
{
//freopen("input.txt","r",stdin);
while(read_list(in_order)){
read_list(post_order);
ans = ,Min = INF;
build(,n-,,n-);
dfs(post_order[n-],);
printf("%d\n",ans);
}
return ;
}
UVA548-Tree(二叉树数组表示)的更多相关文章
- 【日常学习】【二叉树遍历】Uva548 - Tree题解
这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 543. Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- UVA548 Tree (二叉树的遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- UVA548——Tree(中后序建树+DFS)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
随机推荐
- netty源码解解析(4.0)-8 ChannelPipeline的设计
io.netty.channel.ChannelPipeline 设计原理 上图中,为了更直观地展示事件处理顺序, 故意有规律地放置两种handler的顺序,实际上ChannelInboundHa ...
- java中Map集合的理解
Map |--Hashtable:底层是哈希表数据结构,不可以存入null键null值.该集合是线程同步的.jdk1.0.效率低. |--HashMap:底层是哈希表数据结构,允许使用 null 值和 ...
- [PHP] 算法-删除链表中重复的结点的PHP实现
删除链表中重复的结点: 1.定义两个指针pre和current 2.两个指针同时往后移动,current指针如果与后一个结点值相同,就独自往前走直到没有相等的 3.pre指针next直接指向curre ...
- CentOS6.5安装mysql以及常见问题的解决
前言 最近在学习Linux系统,今天在安装MySQL数据库时出现很多问题,花费了两个小时终于解决,故记录下来以供大家参考.(本人目前还在学习阶段,下面写到的是自己结合网上查到的资料以及各位前辈给出的解 ...
- 【Java深入研究】2、JDK 1.8 LinkedList源码解析
LinkedList是一个实现了List接口和Deque接口的双端链表. 有关索引的操作可能从链表头开始遍历到链表尾部,也可能从尾部遍历到链表头部,这取决于看索引更靠近哪一端. LinkedList不 ...
- Java基础回顾Application(一)
Java Web 中application(应用级) session(会话级) request(请求级) 在JavaWeb 中实现数据共享往往通过定义属性的方法来实现,而什么是属性呢?它类似于Hash ...
- 【读书笔记】iOS-属性中的内存管理参数
一,assign 代表设置时候直接赋值,而不是复制或者保留它. 二,retain. 会在赋值的时候把新值保留.此属性只能用于Object-C对象类型. 三,copy 在赋值时,将新值复制一份,复制工作 ...
- K-Means算法的10个有趣用例
https://www.jianshu.com/p/162c9ec713cf 摘要: 让我们走进K-Means算法的“前世今生”以及和它有关的十个有趣的应用案例. K-means算法具有悠久的历史,并 ...
- (网页)JS中的小技巧,但十分的实用!
转自CSDN: 1.document.write(”"); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4. ...
- android recovery 升级之USB设备挂载
Recovery升级过程,通常会从两个地方获取升级包update.zip升级,一般在线升级,会把升级包下载到cache分区,本地升级会从usb或者tf卡中升级.本文讨论下,本地USB升级时,无法挂载U ...