int TreeSize(struct TreeNode* root)
{
return root == NULL? 0 : TreeSize(root->left) + TreeSize(root->right) +1;
}
void preorder(struct TreeNode* root, int* arr, int *i)//这里i的处理要特别注意
{
if(root == NULL)
return;
arr[(*i)++] = root->val;
preorder(root->left, arr, i);
preorder(root->right, arr, i);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
* returnSize = TreeSize(root);//要先求一下需要malloc的size
int * arr = (int *)malloc(sizeof(int) * (* returnSize));
int i = 0;
preorder(root, arr, &i);
return arr;
}



