这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子。

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题解的更多相关文章

  1. 【日常学习】codevs1287 矩阵乘法题解

    转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看. 先上题目 题目描写叙述 Description 小明近期在为线性代数而头疼,线性代数确实非 ...

  2. UVA548 Tree (二叉树的遍历)

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  3. LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

    题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...

  4. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  5. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  6. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  9. hdu 4605 线段树与二叉树遍历

    思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...

随机推荐

  1. Python内置函数6

    Python内置函数6 1.license() 输出当前python 的license信息 A. HISTORY OF THE SOFTWARE ========================== ...

  2. Python内置函数5

    Python内置函数5 1.format参考前面字符串方法中的format 2.frozenset([iterable]) iterable -- 可迭代的对象,比如列表.字典.元组等等 返回一个冻结 ...

  3. 两周多学完Java 23种设计模式

       最近两周任务不是很繁重,对于一个刚入职4个月的菜鸟来说,学习设计模式并灵活使用简直天方夜谭:但是当我询问我导师需要学点啥的时候?“<Java设计模式>,这个必须要学”,一句简单粗略的 ...

  4. BZOJ 2176 Strange string ——最小表示法

    本来想用来练习后缀自动机的,但是100w有点虚(事实证明确实T掉了). 只好上最小表示法. #include <cstdio> #include <cstring> #incl ...

  5. POJ 1006 生理周期【数论】

    这题是有中文版的(右上角选项卡里把default改成简体中文)然后看到他把biorhythms翻成生理周期我可耻的笑了......23333 如果没有限定从日期d开始,完全可以从第一天起开始计时,因此 ...

  6. uva 11798 相对运动的最小最大距离

    C Dog Distance Input Standard Input Output Standard Output Two dogs, Ranga and Banga, are running ra ...

  7. poj 2987 Firing

    Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10696   Accepted: 3226 Descript ...

  8. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  9. Objective-C NSString的常用用法

    //1.创建常量字符串. NSString *astring = @"This is a String!";   //2.创建空字符串,给予赋值. NSString *astrin ...

  10. grequests----golang的requests库

    github.com/levigross/grequests: A Go "clone" of the great and famous Requests library 特点: ...