【日常学习】【二叉树遍历】Uva548 - Tree题解
这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子。
Uva上不去了,没法測。基本上是依照ruka的代码来的。直接上代码
//Uva548 Tree
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<cctype>
using namespace std; const int maxv=10000+10;
int inorder[maxv],postorder[maxv],l[maxv],r[maxv];
int n;
int best,best_num; bool read_list(int *a){
string line;
if (!getline(cin,line)) return false;
stringstream ss(line);
n=0;
int x;
while (ss>>x) a[n++]=x;
return n>0;
} int build(int l1,int r1,int l2,int r2){
if (l1>r1) return 0;//notice
int root=postorder[r2];
int p=0;
while (inorder[p]!=root) p++;
int cnt=p-l1;
l[root]=build(l1,p-1,l2,l2+cnt-1);
r[root]=build(p+1,r1,l2+cnt,r2-1);
return root;
} void dfs(int now,int sum){
sum+=now;
if ((!l[now])&&(!r[now])){
if ((sum<best_num)||((sum==best_num)&&(now<best))){
best=now;
best_num=sum;
}
}
if (l[now]) dfs(l[now],sum);
if (r[now]) dfs(r[now],sum);
} int main(){
freopen("1.txt","r",stdin);
freopen("2.txt","w",stdout);
while (read_list(inorder)){//every time you read an inorder
read_list(postorder);
build(0,n-1,0,n-1);//build a tree
best_num=1000000000;
dfs(postorder[n-1],0);
cout<<best<<endl;;
}
return 0;
}
五一又要出去培训了。要求相当高。我要抓紧时间学习了。
——长风破浪会有时,直挂云帆济沧海
【日常学习】【二叉树遍历】Uva548 - Tree题解的更多相关文章
- 【日常学习】codevs1287 矩阵乘法题解
转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看. 先上题目 题目描写叙述 Description 小明近期在为线性代数而头疼,线性代数确实非 ...
- UVA548 Tree (二叉树的遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- poj2255 (二叉树遍历)
poj2255 二叉树遍历 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descripti ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- hdu 4605 线段树与二叉树遍历
思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...
随机推荐
- DOM tiny-demo
<script type="text/javascript" language="javascript">var i = 4; function a ...
- vs系列自带的localdb数据库当做数据库
我在学习mvc4框架的时候,因为是笔记本,觉得装了一个vs就很大了,再装一个sql server 就更麻烦,因此,就想到使用vs自带的localDB,直接生成预览的时候是没有问题的,但是当把mvc4的 ...
- 九度oj题目1008:最短路径问题
题目描述: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. 输入: ...
- bzoj2324 [ZJOI2011]营救皮卡丘 费用流
[ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2653 Solved: 1101[Submit][Status][D ...
- 圆桌聚餐(cogs 729)
«问题描述:假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri(i=1,2,3...m), .会议餐厅共有n张餐桌,每张餐桌可容纳c i(i=1,2...n) 个代表就餐.为了 ...
- poj 2987 Firing
Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10696 Accepted: 3226 Descript ...
- POJ2167 Irrelevant Elements
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description Young cryp ...
- Codeforces891C. Envy
$n \leq 5e5$,$m \leq 5e5$的无向边权图,$q \leq 5e5$个询问,每次问一系列边是否能同时存在于某棵最小生成树上. 一条边在最小生成树上,当比他小的边都加入后,加入他会使 ...
- Virtualization基础
官方文档学习 https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/pdf/Virtualization_G ...
- curl抓取数据
抓取数据的代码: $url='抓取数据的网站路径'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示 ...