• UVa 536 Tree Recovery


      《算法竞赛入门经典》6.3.3的题,给出一个二叉树的先序遍历和中序遍历,求它的后续遍历。

      书中代码对递归的运用,没得说!递归是一个神奇的东西,我现在只是知道它,但却永不好它,还需努力啊。

      代码如下:

    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 
     4 const int maxn = 30;
     5 
     6 void build(int n, char *s1, char *s2, char *s)
     7 {
     8     if(n <= 0)   return;
     9     int p = strchr(s2, s1[0]) - s2;
    10     build(p, s1+1, s2, s);
    11     build(n-1-p, s1+p+1, s2+p+1, s+p);
    12     s[n-1] = s1[0];
    13 }
    14 
    15 int main()
    16 {
    17 #ifdef LOCAL
    18      freopen("in", "r", stdin);
    19 #endif
    20     char s1[maxn], s2[maxn], s[maxn];
    21     while(scanf("%s%s", s1, s2) != EOF)
    22     {
    23         int n = strlen(s1);
    24         build(n, s1, s2, s);
    25         s[n] = '\0';
    26         printf("%s\n", s);
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    线段树学习笔记
    树状数组学习笔记
    P1816 忠诚 ST表模版
    NOIP 2017 D1T2 时间复杂度
    Ubuntu镜像源
    字符串数据结构模板
    白书的一些奇怪模板
    高精度模板
    大整数类模板
    线段树模板1
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3040468.html
Copyright © 2020-2023  润新知