二叉树的前序遍历,不懂这道题让我返回什么(C语言),c++我做对着-灵析社区

万码用户

二叉树前序遍历 ``` c /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* preorderTraversal(struct TreeNode* root, int* returnSize) { int i = 0; //int *arr = (int *)malloc(100*sizeof(int)); if(root) { returnSize[i++] = root->val; preorderTraversal(root->left, returnSize); preorderTraversal(root->right, returnSize); } return returnSize; } ```

阅读量:146

点赞量:0

问AI
注释里面不是提示了吗?大小为 *returnSize 的数组,下面函数的返回值也明确了是一个 int 类型的指针,这个指针指向一个长度为 *returnSize 的 int 数组。 简而言之,你要在返回之前,设置好 *returnSize 的值,返回的是你自己 malloc 的数组