【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<<"
";
    dfs2(1);  cout<<"
";
    dfs3(1);  cout<<"
";
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444705.html