The preorder of the binary tree traverses, do not understand what this question let me return (C language), cPP I do the opposite

binary tree preorder traversal

/**
 * 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[iPP] = root->val;
        preorderTraversal(root->left, returnSize);
        preorderTraversal(root->right, returnSize);
    }
    
    return returnSize;
}
Feb.27,2021

Isn't there a hint in the

comment? The size of the array is * returnSize . The return value of the following function also makes it clear that it is a pointer of type int, which points to an int array of length * returnSize .

in short, you need to set the value of * returnSize before returning, which returns your own array of malloc

.
Menu