问题:
“在相同级别连接节点
给定一个二叉树,连接相同级别的节点。您将获得一个相同的nextRight指针。
最初,所有nextRight指针都指向垃圾值。您的函数应将这些指针设置为指向每个节点的下一个右边。”
我的策略:
我的输出:
运行时错误:
来自abort(3)的运行时ErrorAbort信号(SIGABRT)
我的代码(仅适用于功能):
/*struct Node
{
int data;
struct Node* left;
struct Node* right;
struct Node* nextRight;
}; */
void connect(Node *root)
{
// vector of all node addresses
// for each set nxtrt next ele
// in level order add a nullptr after each level as delimiter
queue<Node*>q;
if(root)
{
q.push(root);
q.push(nullptr);
}
vector<Node*>v;
Node *curr;
while(!q.empty())
{
curr = q.front();
q.pop();
v.push_back(curr);
if(curr == nullptr)
{
q.push(curr);
continue;
}
if(curr->left)
{
q.push(curr->left);
}
if(curr->right)
{
q.push(curr->right);
}
}
for(int i = 0; i < (int)(v.size() - 1); ++i)
{
v[i]->nextRight = v[i+1]; //making each node point to right node
}
}
答案 0 :(得分:2)
在-g中实际使用connect
的地方构建一些可执行文件,例如。 $ g++ node.cpp -g
,然后使用gdb
:$ gdb a.out
运行它。收到异常后,键入bt full
(https://doc.akka.io/api/akka/current/akka/stream/scaladsl/Source.html)。这将向您确切显示错误所在。试图准备一些最小的示例(尽管我相信这不是您的用例):
#include <queue>
using namespace std;
struct Node
{
int data;
struct Node* left;
struct Node* right;
struct Node* nextRight;
};
void connect(Node *root)
{
// vector of all node addresses
// for each set nxtrt next ele
// in level order add a nullptr after each level as delimiter
queue<Node*>q;
if(root)
{
q.push(root);
q.push(nullptr);
}
vector<Node*>v;
Node *curr;
while(!q.empty())
{
curr = q.front();
q.pop();
v.push_back(curr);
if(curr == nullptr)
{
q.push(curr);
continue;
}
if(curr->left)
{
q.push(curr->left);
}
if(curr->right)
{
q.push(curr->right);
}
}
for(int i = 0; i < (int)(v.size() - 1); ++i)
{
v[i]->nextRight = v[i+1]; //making each node point to right node
}
}
int main() {
Node t;
t.data = 5;
connect(&t);
return 0;
}
我得到的错误:
Reading symbols from a.out...done.
(gdb) r
Starting program: /home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
36 if(curr->left)
(gdb) bt
#0 0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
#1 0x0000555555554c79 in main () at test2.cpp:55
(gdb) bt full
#0 0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
q = std::queue wrapping: std::deque with 2 elements = {0x20ce2d8d48550020, 0x0}
v = std::vector of length 5, capacity 8 = {0x7fffffffdd50, 0x0, 0x555555556c70 <__libc_csu_init>, 0x0, 0x20ce258d4c544155}
curr = 0x20ce258d4c544155
#1 0x0000555555554c79 in main () at test2.cpp:55
t = {data = 5, left = 0x0, right = 0x555555556c70 <__libc_csu_init>, nextRight = 0x555555554910 <_start>}
因此,基于36 if(curr->left)
,我们可以判断curr
为NULL,并且您在某些情况下尝试取消引用。
重要只是一个提示:如果您对调试器不满意,可以随时在每一行中添加print
并查看缺少的打印内容。您将能够识别出可能存在错误的行。
答案 1 :(得分:1)
第一个问题是您要向队列中递归添加nullptr,因此queue永远不会为空。如果它为nullptr,则不应该推送curr
if(curr == nullptr)
{
q.push(curr); // remove this line
continue;
}
第二个问题是您将未初始化的指针推入队列
q.push(curr->left);
比下一个尝试访问其成员的周期
// where curr pushed as curr->left previous cycle
if(curr->left)
为指针成员提供默认值将是解决方案
struct Node
{
int data;
struct Node* left = nullptr;
struct Node* right = nullptr;
struct Node* nextRight = nullptr;
};