C语言
#define NULL ((void *)0)
// 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().
*/
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int* res = (int*)malloc(sizeof(int)*101);
*returnSize=0;
preorder(root, res, returnSize);
return res;
}
void preorder(struct TreeNode* root, int* res, int* resSize){
if(!root)
return;
res[(*resSize)++]=root->val;
preorder(root->left, res, resSize);
preorder(root->right, res, resSize);
}