【codevs3143】二叉树的序遍历

problem

solution

codes

#include<iostream>
using namespace std;
const int maxn = 110;
int tree[maxn][2];
void dfs1(int now){
    cout<<now<<" ";
    if(tree[now][0])dfs1(tree[now][0]);
    if(tree[now][1])dfs1(tree[now][1]);
}
void dfs2(int now){
    if(tree[now][0])dfs2(tree[now][0]);
    cout<<now<<" ";
    if(tree[now][1])dfs2(tree[now][1]);
}
void dfs3(int now){
    if(tree[now][0])dfs3(tree[now][0]);
    if(tree[now][1])dfs3(tree[now][1]);
    cout<<now<<" ";
}
int main(){
    int n;  cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>tree[i][0]>>tree[i][1];
    dfs1(1);  cout<<"\n";
    dfs2(1);  cout<<"\n";
    dfs3(1);  cout<<"\n";
    return 0;
}
posted @ 2018-06-05 21:13  gwj1139177410  阅读(123)  评论(0编辑  收藏  举报
选择