zoukankan      html  css  js  c++  java
  • 从先序中序重建二叉树输出层序后序

    #include <iostream>
    #include <vector>
    #include <string>
    #include <queue>
    #include <cstring>
    #define MAX 10000
    using namespace std;
    
    struct tree
    {
        int data;
        tree* left;
        tree* right;
        tree()
        {
            data = 0;
            left = NULL;
            right = NULL;
        }
    };
    tree* root;
    int front[100000];
    int mid[100000];
    int n;
    tree* build(int *front,int *mid,int n)
    {
        if(n == 0)
            return NULL;
        int x = 0;
        while(front[0]!=mid[x])
            x++;
        tree *t = new tree();
        t->data = mid[x];
        t->left = build(front+1,mid,x);
        t->right = build(front+1+x,mid+1+x,n-x-1);
        return t;
    }
    
    void order(tree* t)
    {
        queue<tree *> tmp;
        tmp.push(t);
        while(!tmp.empty())
        {
            tree * tt;
            tt = tmp.front();
            tmp.pop();
            cout<<tt->data<<" ";
            if(tt->left != NULL)
                tmp.push(tt->left);
            if(tt->right != NULL)
                tmp.push(tt->right);
        }
    }
    void PostOrder(tree* t)
    {
        if(t)
        {
            PostOrder(t->left);
            PostOrder(t->right);
            cout<<t->data<<" ";
        }
    }
    int main()
    {
        cin>>n;
        for (int i = 0; i < n; ++i)
            cin>>front[i];
        for (int i = 0; i < n; ++i)
            cin>>mid[i];
        root = build(front,mid,n);
        order(root);
        cout<<endl;
        PostOrder(root);
        return 0;
    }
  • 相关阅读:
    Ueditor之SAE移植
    SAE flask及其扩展 bug指南
    SAE 安装未包含的第三方依赖包
    Bootstrap 和 LESS
    Device.js——检测设备平台、操作系统的Javascript 库
    Flask 富文本编辑器
    DDoS攻击
    WPF之数据绑定
    参数测试
    总结 一下UML 类图的关系
  • 原文地址:https://www.cnblogs.com/syzyaa/p/14135233.html
Copyright © 2011-2022 走看看