0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树

关键点理解这段代码

int build(int L1,int R1,int L2,int R2)
{
    //printf("built:\n");
    ;//空树
    int root=post_order[R2];
    int p=L1;
    while(in_order[p] != root) p++;
    int  cnt = p-L1;//左子树的结点个数
    lch[root]=build(L1,p-,L2,L2+cnt-);
    rch[root]=build(p+,R1,L2+cnt,R2-);
    return root;
}

1.剩下的就是递归了 注意一下递归边界是  到达叶子结点 即左右子树均为空的结点 就行了

 if(!lch[v] && !rch[v])
 #include <cstdio>
 #include <iostream>
 #include <cstring>
 #include <sstream>
 #include <algorithm>
 using namespace std;
  + ;
 int post_order[maxn],in_order[maxn],lch[maxn],rch[maxn];
 int n,ans_sum,ans_v;

 bool input(int*a)
 {
     string s;
     //getline(cin,s);
     if(!getline(cin,s)) return false;
     stringstream ss(s);
     n=;
     int x;
     while(ss>>x) a[n++]=x;
     ;
 }

 int build(int L1,int R1,int L2,int R2)
 {
     //printf("built:\n");
     ;//空树
     int root=post_order[R2];
     int p=L1;
     while(in_order[p] != root) p++;
     int  cnt = p-L1;//左子树的结点个数
     lch[root]=build(L1,p-,L2,L2+cnt-);
     rch[root]=build(p+,R1,L2+cnt,R2-);
     return root;
 }
 void dfs(int v,int sum)
 {
     sum+=v;
     if(!lch[v] && !rch[v])
     {
         if(sum < ans_sum)
             ans_sum = sum,ans_v=v;
         else if(sum == ans_sum && v < ans_v)
             ans_v = v;
     }
     if(lch[v]) dfs(lch[v],sum);
     if(rch[v]) dfs(rch[v],sum);
 }
 int  main()
 {
     while(input(in_order))
     {
         input(post_order);
         build(,n-,,n-);
         ans_sum=;
         dfs(post_order[n-],);
         printf("%d\n",ans_v);
     }
     ;
 }

Uva 548 Tree的更多相关文章

  1. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  2. UVa 548 Tree(二叉树最短路径)

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

  3. UVa 548 Tree (建树+前序后序)

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

  4. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  5. UVA 548 Tree 建树

    题意: 输入中序和后序的权值,输出哪个叶子使它到根的路径上权和最小. 思路: 输入后建树,然后dfs求最小的叶子. #include<iostream> #include<cstdi ...

  6. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

  7. UVA - 548 Tree(二叉树的递归遍历)

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  8. UVa 548 Tree(中序遍历+后序遍历)

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

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

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

随机推荐

  1. 在SpringMVC中获取request对象

    1.注解法 @Autowired private  HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...

  2. 【leetcode】 Unique Binary Search Trees II (middle)☆

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. iOS应用架构谈(一):架构设计的方法论

    当我们讨论客户端应用架构的时候,我们在讨论什么? 其实市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: 简单来说就是调API,展示页面,然后跳转到别的地方再调API,再展示页面. 那这有什么好 ...

  4. 检查Android系统版本

    <script type="text/javascript"> $(function(){ var userAgent = navigator.userAgent; v ...

  5. fork与vfork的区别与联系

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 2. fo ...

  6. TCP/IP五层模型

    (2)TCP/IP五层模型的协议   应用层 传输层 网络层 数据链路层 物理层   物理层:中继器.集线器.还有我们通常说的双绞线也工作在物理层 数据链路层:网桥(现已很少使用).以太网交换机(二层 ...

  7. iOS - 二维码扫描和应用跳转

    序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如ZXing或者ZBar.使用时集成麻烦, ...

  8. service(启动方式)

  9. JQ AJAX

    用AJAX方法不刷新网页使用下拉列表连接数据库 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  10. Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建

    1.开发工具使用: MyEclipse或Eclipse,数据库使用Oracle.需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理). 2.初始环境配置: ...