PAT 甲级1099 构建二叉树形状时使用递归出现段错误


#include<iostream>
using namespace std;
struct Tree
{
    int data;
    Tree* nl;
    Tree* nr;
};
struct Record
{
    int left;
    int right;
};
int i, j;
int N;
Record* obj = new Record[N];
int* node = new int[N];
int times = 0;
void build(Tree* nroot, int index)
{
    if (obj[index].left == -1) { nroot->nl = NULL;  }
    else { nroot->nl = new Tree; build(nroot->nl, obj[index].left); }
    if (obj[index].right == -1) { nroot->nr = NULL;  }
    else { nroot->nr = new Tree; build(nroot->nr, obj[index].right); }
    return;
}//出错的函数,但是visual studio 上又能够成功构造
void through(Tree*nroot)
{
    if (nroot == NULL) return;
    else
    {
    through(nroot->nl);
    nroot->data = node[times];
    ++times;
    through(nroot->nr);
    }
}
int main()
{
    cin >> N;
    for (i = 0; i < N; ++i)
        cin >> obj[i].left >> obj[i].right;
    Tree* root = new Tree;
    build(root, 0);
    for (i = 0; i < N; ++i)
        cin >> node[i];
    int temp;
    for (i = 0; i < N - 1; ++i)
        for (j = i + 1; j < N; ++j)
            if (node[i] > node[j])
            {
                temp = node[i];
                node[i] = node[j];
                node[j] = temp;
            }
    through(root);
    Tree** ed = new Tree * [N];
    int current = 0, tail = 1;
    ed[0] = root;
    while (tail < N)
    {
        if (ed[current]->nl != NULL) ed[tail] = ed[current]->nl, ++tail;
        if (ed[current]->nr != NULL) ed[tail] = ed[current]->nr, ++tail;
        ++current;

    }
    cout << (ed[0]->data);
    for (i = 1; i < N; ++i)
        cout << ' ' << (ed[i]->data);
    return 0; 
}
实在想不出来,向大佬们求助,谢谢!

是不是递归的时候树节点出错了,可以分享一下思路

已经解决啦