B3642 WA代码求调

学术版

zhaojiawei1234444 @ 2024-09-18 09:01:12

rt B3642

#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int N=100100;
int n;
struct Node{
    int l,r;
}tree[N];
void pre_order(int x){
    cout<<x<<" ";
    if(tree[x].l!=0){
        pre_order(tree[x].l);
    }
    if(tree[x].r!=0){
        pre_order(tree[x].r);
    }
}
void in_order(int x){
    if(tree[x].l!=0){
        pre_order(tree[x].l);
    }
    cout<<x<<" ";
    if(tree[x].r!=0){
        pre_order(tree[x].r);
    }
}
void post_order(int x){
    if(tree[x].l!=0){
        pre_order(tree[x].l);
    }
    if(tree[x].r!=0){
        pre_order(tree[x].r);
    }
    cout<<x<<" ";   
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>tree[i].l>>tree[i].r;
    }
    pre_order(1);
    cout<<"\n";
    in_order(1);
    cout<<"\n";
    post_order(1);

}

by ztyo_zysclown @ 2024-09-18 09:02:28

@zhaojiawei1234444 你直接复制过来改函数名了吗


by zhaojiawei1234444 @ 2024-09-18 09:03:57

@ztyo_zysclown 无语


by zhaojiawei1234444 @ 2024-09-18 09:04:21

我自己搜的


by zhaojiawei1234444 @ 2024-09-18 09:04:54

@ztyo_zysclown 所以你看出来我哪错了吗


by zbf142103 @ 2024-09-18 09:05:25

本人AC代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+100;
struct node{
    int l,r;
}e[N];
int n;
void dfs(int x,int z){
    if(z==1) cout<<x<<" ";
    if(e[x].l) dfs(e[x].l,z);
    if(z==2) cout<<x<<" ";
    if(e[x].r) dfs(e[x].r,z);
    if(z==3) cout<<x<<" ";
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>e[i].l>>e[i].r;
    }
    dfs(1,1);
    cout<<"\n";
    dfs(1,2);
    cout<<"\n";
    dfs(1,3);
    return 0;
}

by zhaojiawei1234444 @ 2024-09-18 09:07:02

@zbf142103 我要我的AC,please


by zbf142103 @ 2024-09-18 09:07:15

楼主确实是copy 的题解(在现场)


by ztyo_zysclown @ 2024-09-18 09:12:20

你他妈三个函数递归全用的前序的


by ztyo_zysclown @ 2024-09-18 09:12:28

@zhaojiawei1234444


by zhaojiawei1234444 @ 2024-09-18 09:16:31

@ztyo_zysclown 中序


| 下一页