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

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. Hive 执行sql命令报错

    Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxExce ...

  2. Python --写excel

    # -*- coding: UTF-8 -*- import xlwt import StringIO # 将数据保存成excel def write_data(data, tname): file ...

  3. 原 .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)

    大家都说反射耗性能,但是到底有多耗性能,哪些反射方法更耗性能:这些问题却没有统一的描述. 本文将用数据说明反射各个方法和替代方法的性能差异,并提供一些反射代码的编写建议.为了解决反射的性能问题,你可以 ...

  4. HDU 5073 Galaxy ——乱搞

    [题目分析] 练习赛的T1. 只要看懂样例就可以猜结论了. 然后大胆猜测剩下的星星是一段,其余的都移到重心上去. 所以只要把计算的式子变形一下就很好维护了. 居然没有1A [代码] #include ...

  5. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  6. cf701E Connecting Universities

    Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's poss ...

  7. oracle禁止插入、延迟插入方法

    DATE_ADD(DATE_ADD(curdate(),INTERVAL +6 HOUR),INTERVAL +6 DAY) mysql取当前日期后6天,截止到6点钟的方法 --直接报错 CREATE ...

  8. Spring入门之setter DI注入

    1.新建Java项目导入依赖jar包,参考前一章 2.以不同文件格式输出为例 3.定义接口IOutputGenerator.java package com.spring.output; public ...

  9. *Codeforces587E. Duff as a Queen

    $n \leq 200000$的序列,支持以下$q \leq 4e4$个操作:区间异或$k$:查询区间能异或出多少不同的数.数字$0 \leq a_i \leq 1e9$. 大概是要区间线性基.区间修 ...

  10. 蜥蜴 BZOJ 1066

    蜥蜴 [问题描述] 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距 ...