链接:. - 力扣(LeetCode)【点击即可跳转】
使用c语言来实现。
代码展示为:
/*** Note: The returned array must be malloced, assume caller calls free().*/int TreeSize( struct TreeNode* root)
{return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
}
void _prevOrder( struct TreeNode*root,int* a,int* pi)
{if(root==NULL){return;}a[*pi]=root->val;++(*pi);_prevOrder(root->left,a,pi);_prevOrder(root->right,a,pi);
}int* preorderTraversal( struct TreeNode* root, int* returnSize)
{int size=TreeSize(root);int* a=(int*)malloc(size*sizeof(int));int i=0;_prevOrder(root,a,&i);*returnSize=size;return a;
}