zoukankan      html  css  js  c++  java
  • F

    Description

    Download as PDF

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

    Input 

    The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

    Output 

    For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

    Sample Input 

    3 2 1 4 5 7 6
    3 1 2 5 6 7 4
    7 8 11 3 5 16 12 18
    8 3 11 7 16 18 12 5
    255
    255
    

    Sample Output 

    1
    3
    255
    

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <cstdlib>
    15 #include <sstream>
    16 using namespace std;
    17 const int INF=0x5fffffff;
    18 const double EXP=1e-8;
    19 const int mod=100000007;
    20 const int MS=10005;
    21 
    22 int in_order[MS],post_order[MS];
    23 int lch[MS],rch[MS];
    24 int n,best,best_sum;
    25 
    26 bool input()
    27 {
    28     string str;
    29     if(!getline(cin,str))
    30         return false;
    31     stringstream ss(str);
    32     int cnt=0;
    33     int x;
    34     while(ss>>x)
    35         in_order[cnt++]=x;
    36     getline(cin,str);
    37     stringstream  s(str);
    38     n=cnt;
    39     cnt=0;
    40     while(s>>x)
    41         post_order[cnt++]=x;
    42     best_sum=INF;
    43     return true;
    44 }
    45 
    46 int build(int L1,int R1,int L2,int R2)
    47 {                                       //返回树根
    48     if(L1>R1)
    49         return 0;
    50     int root=post_order[R2];
    51     int p=L1;
    52     while(in_order[p]!=root)
    53             p++;
    54     int cnt=p-L1;
    55     lch[root]=build(L1,p-1,L2,L2+cnt-1);
    56     rch[root]=build(p+1,R1,L2+cnt,R2-1);
    57     return root;
    58 }
    59 
    60 void dfs(int root,int sum)
    61 {
    62     sum+=root;
    63     if(!lch[root]&&!rch[root])
    64     {
    65         if(sum<best_sum||(sum==best_sum&&root<best))
    66         {
    67             best=root;
    68             best_sum=sum;
    69         }
    70     }
    71     if(lch[root])
    72         dfs(lch[root],sum);
    73     if(rch[root])
    74         dfs(rch[root],sum);
    75 }
    76 
    77 int main()
    78 {
    79     while(input())
    80     {
    81         build(0,n-1,0,n-1);
    82         dfs(post_order[n-1],0);
    83         cout<<best<<endl;
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7
    CentOS7安装使用MySQL
    使用passenger在Centos7部署Puma+Nginx+Ruby on Rails
    DOS和UNIX文本文件之间相互转换的方法
    CentOS7安装vim7.4
    Python多版本共存之pyenv
    我的Shell + VIM配置
    CentOS7安装Python3.5
    CentOS7系统下搭建Jenkins环境
    Windows系统下搭建Jenkins环境
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4321512.html
Copyright © 2011-2022 走看看