您的位置:首页 > 其它

Codves 3143 二叉树的序遍历 递归

2016-10-10 17:29 134 查看
没想到这个题这么水啊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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  递归 二叉树 遍历