cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
void preorder(struct TreeNode* proot,int* parr,int* size)
{
if(proot==NULL)
return;
parr[(*size)++]=proot->val;
preorder(proot->left,parr,size);
preorder(proot->right,parr,size);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* arr=(int*)malloc(sizeof(int)*2000);
* returnSize=0;
preorder(root,arr,returnSize);
return arr;
}