zoukankan      html  css  js  c++  java
  • uva 548 Tree

    题意 :给定一棵树的中序遍历和后序遍历,让你求出这棵树根节点到叶子节点和最小的叶子节点的值(如果最小和有几个,选叶子节点值最小的)

    解题思路:(1)建树   (2)划分遍历(同白书,后序遍历每一段最后一个值都是这个子树的根,所以从中序遍历中找对应的根求解)  我用的是方法2

    解题代码:

    // File Name: uva548.c
    // Author: darkdream
    // Created Time: 2013年05月19日 星期日 19时52分46秒
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    #include<limits.h>
    int a[10005];
    int b[10005];
    int maxstep,minnum;
    void find(int ai , int aj ,int  bi,int bj, int step)
    {
       if(ai == aj)
       {
         if(step +a[ai] < maxstep)
         { 
           maxstep = step +a[ai];
           minnum = a[ai];
         }
         if(step +a[ai] == maxstep && a[ai] <minnum)
             minnum = a[ai];
         return ;
       }
       int i ;
       for(i = ai; i <= aj ;i ++)
       {
          if(a[i] == b[bj])
              break;
       }
       if(i!= ai)
       {
         find(ai,i-1,bi,bi+(i-1-ai),step+a[i]);
    
       }
       if(i!= aj)
       {
         find(i+1,aj,bi+(i-ai),bj-1,step+a[i]);
       }
     return;
    }
    int main(){
    
       //freopen("/home/plac/problem/input.txt","r",stdin);
       //freopen("/home/plac/problem/output.txt","w",stdout);
       int temp ;
       while(scanf("%d",&temp) != EOF)
       {
        maxstep =INT_MAX;
        minnum = INT_MAX;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int t ;
        char c;
        c = getchar();
        t = 1;
        a[t] = temp;
        while(1)
        {
            if(c == '\n')
                break;
            t++;
            scanf("%d",&a[t]);
            c = getchar();
        }
        
        for(int i = 1;i <= t; i ++)
            scanf("%d",&b[i]);
        find(1,t,1,t,0);
        printf("%d\n",minnum);
       }
    return 0 ;
    }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    skywalking监控配置tomcat的参数
    weblogic启动受管理节点
    JavaScript中的数组遍历forEach()与map()方法以及兼容写法
    ajax与HTML5 history pushState/replaceState实例
    mongoose参考手册
    mongoose
    解决ul里最后一个li的margin问题
    前端开发中最常用的JS代码片段
    CSS3精美前端
    60个有用的css代码片段
  • 原文地址:https://www.cnblogs.com/zyue/p/3088098.html
Copyright © 2011-2022 走看看