zoukankan      html  css  js  c++  java
  • Tree Recovery解题报告

    1.解体思路,寻找先序中序后序中顶点的位置,用递归计算。

    以下是解体思路:(摘自http://www.cnblogs.com/allensun/archive/2010/11/05/1870214.html

    preorder的第一个节点D就是这棵树的根节点,即为postorder中的最后一个元素 
    然后在inorder中找到该节点D,其左边为ABC为该根节点左子树的inorder,右边EFG为右子树inorder 
    对应的preorder中BAC为该根节点左子树的perorder,EGF为右子树preorder 
    再递归对左右子树应用同样的方法 

    2.体会递归函数的写法。确定使用递归函数之后,对第一次递归时的计算顺序编写程序,难点是参数的设置

    3.使用引用,并加const关键字,表示引用变量不能改变原变量的值。


    #include<stdio.h>
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    string pre, in;
    
    void Creat_Post(const string &pre, const string &in, string &post, int end)
    {
        if (pre == "")
            return;
        post[end] = pre[0];
        unsigned int root = in.find(pre[0]);
        int leftL = root;
        int rightL = pre.size() - leftL - 1;
        Creat_Post(pre.substr(1 + leftL), in.substr(root + 1), post, end - 1);
        Creat_Post(pre.substr(1, leftL), in.substr(0, leftL), post, end - 1 - rightL);
    }
    
    int main()
    {
        while(cin >> pre >> in)
        {
            int len = pre.size();
            string post(len,'0');
            Creat_Post(pre, in, post, len -1);
            cout << post << endl;
        }
        return 0;
    }
    


  • 相关阅读:
    USACO6.4-The Primes
    ZOJ2112--Dynamic Rankings (动态区间第k大)
    Havel定理
    HDU5107---K-short Problem (线段树区间 合并、第k大)
    POJ2104-- K-th Number(主席树静态区间第k大)
    poj2409 & 2154 polya计数+欧拉函数优化
    CodeForces
    HDU
    HDU
    Gym
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188940.html
Copyright © 2011-2022 走看看