程序目的:力扣迭代实现N叉树的前序遍历
运行环境:VS2019 C语言编写
问题:力扣提交时出现了 Line 207: Char 3: runtime error: load of null pointer of type 'int' [Serializer.c]错误
但是VS2019自己调试时没有任何问题。
力扣原题链接:https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/
力扣代码提交代码:
#define MAXSIZE 5000
int* preorder(struct Node* root, int* returnSize) {
if(root==NULL){
*returnSize=0;
return NULL;
}
int top=0;
int Res[MAXSIZE]={0};
struct Node* S[MAXSIZE]={NULL};
S[top++]=root;
*returnSize=0;
while(top>0){
struct Node* tmp=S[--top];
Res[(*returnSize)++]=tmp->val;
int NumSize=tmp->numChildren-1;
for(int i=NumSize;i>=0;--i){
if((tmp->children)[i]){
S[top++]=(tmp->children)[i];
}
}
}
return Res;
}
VS2019测试代码:
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 400
typedef struct BiTNode {
struct BiTNode** Children;
int numChildren;
int val;
}BiTnode;
void CreateBiTree(BiTnode **root){
int val, numchildren;
printf("输入节点的值和节点的度:\n");
scanf_s("%d%d", &val, &numchildren);
*root = (BiTnode*)malloc(sizeof(BiTnode));
if (*root == NULL) return;
(*root)->val = val;
(*root)->numChildren = numchildren;
if ((*root)->numChildren <= 0) {
(*root)->Children = NULL;
}
else
{
(*root)->Children = (BiTnode**)malloc(sizeof(BiTnode*) * numchildren);
if ((*root)->Children == NULL) return;
for (int i = 0; i < (*root)->numChildren; i++) {
CreateBiTree(&((*root)->Children)[i]);
}
}
}
int* PreOrder(BiTnode *root,int *returnsize){
if (root == NULL)
{
*returnsize = 0;
return NULL;
};
int Res[MAXSIZE]={0};
int top = 0;
BiTnode* S[MAXSIZE] = {0};
S[top++] = root;
*returnsize = 0;
while (top>0) {
BiTnode* tmp = S[--top];
Res[(*returnsize)++] = tmp->val;
int NumSize = tmp->numChildren-1;
for (int i = NumSize; i >= 0;--i) {
if ((tmp->Children)[i])
{
S[top++] = (tmp->Children)[i];
}
}
}
return Res;
}
int main() {
BiTnode* root;
CreateBiTree(&root);
int returnsize;
int* Res = PreOrder(root,&returnsize);
for (int i = 0; i < returnsize; i++)
printf("%d ", Res[i]);
return 0;
}