当我做出语句 root->right->right = newnode(7);注释,编译器显示运行时错误。谁能解释为什么?
When I make the statement root->right->right = newnode(7); a comment, the compiler shows a runtime error.Can anyone explain why?
我正在编写用于计算c 二进制树中叶片路径总数的代码。
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<queue>
using namespace std;
struct node
{int data;
struct node *left;
struct node *right;
};
struct node* newnode(int data)
{struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int root_to_leaf_paths(struct node *root)
{if(root->left == NULL && root->right == NULL)
return 1;
else if(root == NULL)
return 0;
else
return(root_to_leaf_paths(root->left)+root_to_leaf_paths(root->right));
}
int main()
{
struct node *root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
root->right->left = newnode(6);
当我添加以下行注释时,编译器会显示运行时错误。
**//root->right->right = newnode(7);**
printf("%d",root_to_leaf_paths(root));
}
条件顺序向后:
if(root->left == NULL && root->right == NULL)
return 1;
else if(root == NULL)
return 0;
else
return(root_to_leaf_paths(root->left)+root_to_leaf_paths(root->right));
您需要首先确定root
在访问root->left
等之前不是null。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- EASTL矢量<向量<int>>连续的
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果