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. Oracle 修改现有列的数据类型

    如果表中有数据,Oracle是不能修改其数据类型的.但可以通过新建一个临时列,将要修改列的数据复制到临时列中,删除原列再修改临时列的名字.这样说好像有点拗口,分步解说一下. 表AC_REG中有列:is ...

  2. 在CMD窗口中使用javac和java命令进行编译和执行带有包名的具有继承关系的类

    一.背景 最近在使用记事本编写带有包名并且有继承关系的java代码并运行时发现出现了很多错误,经过努力一一被解决,今天我们来看一下会遇见哪些问题,并给出解决办法. 二.测试过程 1.父类代码 pack ...

  3. SQL Server 查询时间段内数据

    方式一: ALTER Proc [dbo].[usp_Rpt_AcctTypeAudit] @FromDate datetime=null, -- yyyy-mm-dd (may change in ...

  4. 如何用Endnote导入你要用的格式

    在Google搜索某一个期刊名 ens格式的文件,下载,然后放入endnote的文件夹中(C:\Program Files (x86)\EndNote X7\Styles) 然后将其导入即可

  5. 在程序中使用geos.dll

    1 在项目->property->configuration properties->c/c++->general->additional include directo ...

  6. 解决eclipse manven项目添加不了maven dependencis

    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"& ...

  7. 内核中用于数据接收的结构体struct msghdr(转)

    内核中用于数据接收的结构体struct msghdr(转) 我们从一个实际的数据包发送的例子入手,来看看其发送的具体流程,以及过程中涉及到的相关数据结构.在我们的虚拟机上发送icmp回显请求包,pin ...

  8. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  9. Xcode - 修改变量名、类名及字符串的替换操作

    在做iOS开发代码优化的工作时,优化代码结构之前,我们应该先整理好工程的外貌,将文件和类的命名进行规范,在Xcode中为我们提供了方便而强大的名称修改功能. 第一步:修改类名 将鼠标点击放在类的名称上 ...

  10. UITableView和UICollectionView的方法学习一

    参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...