Codves 3143 二叉树的序遍历 递归

来源:互联网 发布:javascript写的小游戏 编辑:程序博客网 时间:2024/06/05 19:48

没想到这个题这么水啊QAQ。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int MAXN = 5000 + 50;int lson[MAXN], rson[MAXN];void xian(int x){    if(!x)  return;    printf("%d ", x);    xian(lson[x]);    xian(rson[x]);}void zhong(int x){    if(!x)  return;    zhong(lson[x]);    printf("%d ", x);    zhong(rson[x]);}void hou(int x){    if(!x)  return;    hou(lson[x]);    hou(rson[x]);    printf("%d ", x);}int main(){    int n;    scanf("%d", &n);    for(int i = 1; i <= n; i ++)    {        int l, r;        scanf("%d%d", &l, &r);        lson[i] = l;        rson[i] = r;    }    xian(1);    puts("");    zhong(1);    puts("");    hou(1);    puts("");    return 0;}
1 0
原创粉丝点击