力扣提交错误:load of null pointer of type 'int' [__Serializer__.c]

0

程序目的:力扣迭代实现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;
}
ava
凤栖梧

2021-1-8

0

VS2019 编译你的代码,已经给出了一个非常严重的警告:

Line 51,Warning C4172:returning address of local variable or temporary: Res

忽视警告,导致你遇到了这个严重的问题。

ava
慢羊羊

2021-1-8

是的,是这个警告的问题,谢谢大佬 - 凤栖梧 2021-1-9