题目链接:https://vjudge.net/problem/UVA-548

题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根的路径上的权值和最小。如果有多个解,该叶子本身的权值应尽量小。输入中每两行表示一棵树,其中第一行为中序遍历,第二行为后续遍历。

看代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
#include<sstream>
using namespace std;
const int maxv=+;
int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];
int n;
int best,best_sum;//目前为止最优解和对应的权值和
bool read_list(int *a)
{ string line;
if(!getline(cin,line)) return false;//文件结束
stringstream ss(line);
n=;
int x;
while(ss>>x) a[n++]=x;//应用字符串流进行输入
return n>;
}
int build(int L1,int R1,int L2,int R2)
{
if(L1>R1) return ;//空树
int root=post_order[R2];//后序遍历最后一个结点就是树根
int p=L1;
while(in_order[p]!=root) p++;//在中序遍历中找到根结点所在的位置
int cnt1=p-L1;//左子树结点的个数
lch[root]=build(L1,p-,L2,L2+cnt1-);
rch[root]=build(p+,R1,L2+cnt1,R2-);
return root;
}
void dfs(int u,int sum)
{
sum+=u;
if(!lch[u]&&!rch[u])//代表叶子结点
{
if(sum<best_sum||(sum==best_sum&&u<best))
{
best=u;
best_sum=sum;
}
}
if(lch[u]) dfs(lch[u],sum);
if(rch[u]) dfs(rch[u],sum);
}
int main()
{
while(read_list(in_order))
{
read_list(post_order);
build(,n-,,n-);
best_sum=;
dfs(post_order[n-],);
cout<<best<<endl;
}
return ;
}

Tree UVA - 548(二叉树递归遍历)的更多相关文章

  1. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

  2. 数据结构之二叉树篇卷二 -- 二叉树递归遍历(With Java)

    一.先序递归遍历(Preorder Recursive Traversal) 1.1 算法 首先需要明确的是这里的序是针对 root 节点而言的.故先序即先“访问”根节点,其次“访问”其左右节点. 1 ...

  3. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

  4. UVa 548 (二叉树的递归遍历) Tree

    题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍 ...

  5. Uva 548 二叉树的递归遍历lrj 白书p155

    直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...

  6. java 二叉树递归遍历算法

    //递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...

  7. python实现二叉树递归遍历与非递归遍历

    一.中序遍历 前中后序三种遍历方法对于左右结点的遍历顺序都是一样的(先左后右),唯一不同的就是根节点的出现位置.对于中序遍历来说,根结点的遍历位置在中间. 所以中序遍历的顺序:左中右 1.1 递归实现 ...

  8. Tree UVA - 548

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

  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

随机推荐

  1. 无需知道类名,就可以启动apk

    查看官方文档,发现这样一个方法: public abstract Intent getLaunchIntentForPackage (String packageName) 这个方法名就说明:根据包名 ...

  2. vue配置vue-router

    首先理清一下几个路由的基础概念: 1)route是一条路由,也就是映射,即A按钮→A内容,以数组形式存储 2)toutes:[]是一组路由,里面包含了若干条route,即route[{A按钮→A内容} ...

  3. Oracle中的学习笔记

    1.使用 ||来连接字符串 select CARD_ID ||','||CARD_TYPE as qqq from CARDS t 2.DISTINCT (唯一不重复) select DISTINCT ...

  4. mysql服务启动不了解决方法

    sudo lsof |grep deleted  找占用大的kill一下,  一般是tomcat log和zookeeper的out比较吃磁盘 du -h --max-depth=1 / 今天作死,想 ...

  5. Python3中开发目录的引用

    Python3中开发目录的引用 import os,sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ...

  6. [SinGuaRiTy] 2017-07-24 NOIP2015 模拟赛

    [SinGuLaRiTy-1030] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有题目: Time Limit: 1s | Mem ...

  7. 1、OpenCV Python 图像加载和保存

    __author__ = "WSX" import cv2 as cv # 这里的文件是图片或者视频 def Save_File( image ): cv.imwrite(&quo ...

  8. EM最大期望算法

    [简介] em算法,指的是最大期望算法(Expectation Maximization Algorithm,又译期望最大化算法),是一种迭代算法,在统计学中被用于寻找,依赖于不可观察的隐性变量的概率 ...

  9. Python导入命令 import from

    一 module通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc" ...

  10. 邮件服务器启动postfix时的问题:master 已死,但 pid 文件仍存

    [root@linux115 ~]# service postfix restart  // 重启postfix关闭 postfix:[失败]  启动 postfix: [确定][root@linux ...