zoukankan      html  css  js  c++  java
  • 3143 codevs 二叉树的序遍历

    题目描述 Description

    求一棵二叉树的前序遍历,中序遍历和后序遍历

    输入描述 Input Description

    第一行一个整数n,表示这棵树的节点个数。

    接下来n行每行2个整数L和R。第i行的两个整数Li和Ri代表编号为i的节点的左儿子编号和右儿子编号。

    输出描述 Output Description

    输出一共三行,分别为前序遍历,中序遍历和后序遍历。编号之间用空格隔开。

    样例输入 Sample Input

    5

    2 3

    4 5

    0 0

    0 0

    0 0

    样例输出 Sample Output

    1 2 4 5 3

    4 2 5 1 3

    4 5 2 3 1

    数据范围及提示 Data Size & Hint

    n <= 16

    分类标签 Tags 点此展开 

     
    代码如下:
    /*递归思想*/

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct node
    {
    int l,r;
    }t[20];
    void qian(int root)
    {
    if(!root)return;
    printf("%d ",root);
    qian(t[root].l);
    qian(t[root].r);
    }
    void zhong(int root)
    {
    if(!root)return;
    zhong(t[root].l);
    printf("%d ",root);
    zhong(t[root].r);
    }
    void hou(int root)
    {
    if(!root)return;
    hou(t[root].l);
    hou(t[root].r);
    printf("%d ",root);
    }
    int main()
    {
    int n,x,y;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    scanf("%d%d",&x,&y);
    t[i].l=x;
    t[i].r=y;
    }
    qian(1);
    cout<<endl;
    zhong(1);
    cout<<endl;
    hou(1);
    cout<<endl;
    return 0;
    }

  • 相关阅读:
    无缝轮播图
    瀑布流之ajax
    进阶版轮播图
    桌面特效
    3D模型文字动画
    Razor 常用方法
    easyui常用
    C#
    Redis设置记录
    日志三剑客ELK
  • 原文地址:https://www.cnblogs.com/zzyh/p/6602189.html
Copyright © 2011-2022 走看看