zoukankan      html  css  js  c++  java
  • Tree Recovery(前序中序求后序)

    Tree Recovery
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14640   Accepted: 9091

    Description

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. 
    This is an example of one of her creations: 

    D
    /
    /
    B E
    /
    /
    A C G
    /
    /
    F

    To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. 
    She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it). 

    Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree. 
    However, doing the reconstruction by hand, soon turned out to be tedious. 
    So now she asks you to write a program that does the job for her! 

    Input

    The input will contain one or more test cases. 
    Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) 
    Input is terminated by end of file. 

    Output

    For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

    Sample Input

    DBACEGF ABCDEFG
    BCAD CBAD
    

    Sample Output

    ACBFGED

    CDAB

    已知二叉树的前序后序遍历求后序遍历。
    代码:
    注意in的范围的变化和pre一样
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    typedef struct Tree{
        Tree *l, *r;
        char value;
        Tree(){
            l = NULL;
            r = NULL;
        }
    }Tree;
    int find(char c, char *in){
        for(int i = 0; in[i]; i++){
            if(c == in[i]){
                return i;
            }
        }
    }
    void make(int n, char *pre, char *in, Tree *&t){
        if(n <= 0)return;
        int k = find(pre[0], in);
        t = new Tree();
        
        t->value = pre[0];
        make(k, pre + 1, in, t->l);
        make(n - 1 - k, pre + k + 1, in + k + 1, t->r);
    }
    void postVisit(Tree *tree){
        
        if(tree->l)
            postVisit(tree->l);
        if(tree->r)
            postVisit(tree->r);
        printf("%c", tree->value);
    }
    int main(){
        char pre[30],in[30];
        while(~scanf("%s%s", pre, in)){
            Tree t;
            make(strlen(pre), pre, in, t.l);
            postVisit(t.l);
            puts("");
        }
        return 0;
    }
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    typedef struct Tree{
        Tree *l, *r;
        char value;
        Tree(){
            l = NULL;
            r = NULL;
        }
    }Tree;
    int find(char c, char *in){
        for(int i = 0; in[i]; i++){
            if(c == in[i]){
                return i;
            }
        }
    }
    Tree* make(int n, char *pre, char *in){
        if(n <= 0)return NULL;
        int k = find(pre[0], in);
        Tree *t = new Tree();
        
        t->value = pre[0];
        t->l = make(k, pre + 1, in);
        t->r = make(n - 1 - k, pre + k + 1, in + k + 1);
        return t;
    }
    void postVisit(Tree *tree){
        
        if(tree->l)
            postVisit(tree->l);
        if(tree->r)
            postVisit(tree->r);
        printf("%c", tree->value);
    }
    int main(){
        char pre[30],in[30];
        while(~scanf("%s%s", pre, in)){
            Tree *t;
            t = make(strlen(pre), pre, in);
            postVisit(t);
            puts("");
        }
        return 0;
    }
     
  • 相关阅读:
    动态SQL语句
    Mybatis配置和基于配置的使用
    JQuery封装的ajax、ajax上传文件、JSON对象
    Jsp生命周期、Jsp的使用、JSP隐式对象、EL表达式、JSTL
    原生Ajax
    Servlet上传文件、会话跟踪、Cookies和session的使用及其常用方法
    ResponseBodyAdvice拦截Controller方法默认返回参数,统一处理返回值/响应体
    钉钉机器人
    花瓶安装和使用
    方法入参检测工具类 spring自带org.springframework.util.Assert 通用类
  • 原文地址:https://www.cnblogs.com/handsomecui/p/6622123.html
Copyright © 2011-2022 走看看