C++数组实现二叉树遍历

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

# include
 
struct node
{
    int l,r;
};
struct node tree[100];
int path[100];
int ans;
void init()
{
    int i;
    ans = 0;
    for(i = 0 ; i < 100 ; i ++  )
        tree[i].l = tree[i].r = -1,path[i] = -1;
}
void preOrderTree(int root)
{
    if(root == -1 )
        return ;
    path[ans++] = root;
    preOrderTree(tree[root].l);
     
    preOrderTree(tree[root].r);
     
}
int main()
{
    int T,i,n;
    int root,a,b,c;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&n);
        scanf("%d",&root);
        for(i = 0 ; i < n -1; i ++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(c == 0)
            {
                tree[a].l = b;
            }
            else
            {
                tree[a].r = b;
            }
        }
        preOrderTree(root);
        for(i = 0 ; i < n ; i ++)
            printf("%d%c",path[i],i' ':'/n');
         
    }
    return 0;
}