• wiki 3143 二叉树的前序、中序及后序遍历


    先序遍历:訪问根。遍历左子树。遍历右子树,简称:DLR。

    中序遍历:遍历左子树,訪问根,遍历右子树,简称:LDR。

    后序遍历:遍历左子树,遍历右子树。訪问根。简称:LRD。

    数组搞的:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<set>
    #include<cmath>
    #include<bitset>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define lson i<<1,l,mid
    #define rson i<<1|1,mid+1,r
    #define llson j<<1,l,mid
    #define rrson j<<1|1,mid+1,r
    #define INF 1000000007
    #define seed 13131
    #define seed1 1313
    #define maxn 500005
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    int n,ch[18][2];
    void pre(int i)
    {
        cout<<i<<' ';
        if(ch[i][0]) pre(ch[i][0]);
        if(ch[i][1]) pre(ch[i][1]);
    }
    void mid(int i)
    {
        if(ch[i][0]) mid(ch[i][0]);
        cout<<i<<' ';
        if(ch[i][1]) mid(ch[i][1]);
    }
    void beh(int i)
    {
        if(ch[i][0]) beh(ch[i][0]);
        if(ch[i][1]) beh(ch[i][1]);
        cout<<i<<' ';
    }
    int main()
    {
        scanf("%d",&n);
        int l,r;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&l,&r);
            ch[i][0]=l,ch[i][1]=r;
        }
        pre(1);
        cout<<endl;
        mid(1);
        cout<<endl;
        beh(1);
        cout<<endl;
        return 0;
    }
    


  • 相关阅读:
    Spring Boot
    Spring Boot – Jetty配置
    如何使ESLint在Visual Studio 2019和2017中工作: 2019 v16和2017> = v15.8
    CentOS 7 安装 Nginx
    HTTPS-使用Certbot自动配置Let’s Encrypt证书
    centos7升级内核到最新版本
    [C#.net]Connection Timeout和Command Timeout
    Redis中切换db
    Redis 模糊查询删除操作
    [Abp vNext 源码分析]
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5064725.html
Copyright © 2020-2023  润新知