#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;
}
实在想不出来,向大佬们求助,谢谢!
PAT 甲级1099 构建二叉树形状时使用递归出现段错误
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-深度学习进阶 2021-09-18 19:40关注
是不是递归的时候树节点出错了,可以分享一下思路
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 基于单片机的靶位控制系统
- ¥15 AT89C51控制8位八段数码管显示时钟。
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题
- ¥15 matlab中使用gurobi时报错