【日常学习】【二叉树遍历】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小 ...
随机推荐
- Django之单表的增删改查
books/urls.py """books URL Configuration The `urlpatterns` list routes URLs to vi ...
- openscad 3Dmodels 笔记
参考链接 官方文档 官方文档之--代码 如何快速上手 打开openSCAD后界面如下: 选择其中的examples,从basic看起.配合官方文档中的first step部分,和官方文档--代码写法即 ...
- ssh执行远程服务器脚本 提示php: command not found
ssh执行远程服务器脚本 提示php: command not found 设置环境变量 一台机器作为管理机,来管理其他服务器,并通过key认证,免密码登陆的. 在管理机上通过ssh登陆到其他服务器来 ...
- 【JavaScript 7—基础知识点】:BOM
一.基础知识 1.1,什么是BOM BOM(browser object model):也叫浏览器对象模型,它提供了很多对象,用于访问浏览器的功能.BOM缺少规范,每个浏览器提供商又按照自己想法去扩展 ...
- Python unittest 学习
import unittest class UTest(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper() ...
- web文件上传大小限制
最近在项目中遇到上传文件,对上传文件的大小需要进行限制,这里学习和整理了一下一些常规的文件大小限制的方法. 一般分为两种方式,一种是服务器端判断文件大小进行限制,这种方法的存在明显的缺陷,当用户过多后 ...
- Luogu【P1901】发射站(单调栈)
题目链接 题目说明比自己矮的塔收不到自己的能量,摆明了就是单调栈呗. 把比自己矮的全都从栈里弹出去,于是碰到第一个比自己高的.让他接受自己发射的能量. 当然由于发射站发射的能量有两个方向,所以正反两遍 ...
- Infinite monkey theorem(hdu 3689)
题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. /* KMP+DP 设f[i][j]表示A生成到第i位,此时B串匹配到第j位的概率. 转移方程为f[i+ ...
- 网上找的一篇博文,原文搞错了,应该是\r\n,本文已改正!——回车CR和换行line feed
"回车"(carriage return)和"换行"(line feed)与 ASCII表 关于“回车”(carriage return)和“换行”(line ...
- Redis命令行之Set
一.Redis之Set简介 1. Set是String类型的无序集合(元素成员唯一). 2. Set是通过hash表实现的,添加.删除.查找的复杂度都是O(1). 3. 每个集合最大成员数为232-1 ...