温馨提示:作者的刷题笔记只是作者认为比较好的题,供复习用哈,所以可能比较水哈,主要还是作者太懒了。
创建子函数是因为避免重复开辟数组,然后子函数用于递归。
/**
* 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 BTSize(struct TreeNode* root)
{
if(root==NULL)
return 0;
return BTSize(root->left)+
BTSize(root->right)+1;
}
void _inorderTraversal(struct TreeNode* root,int* arr, int* pi)
{
if(root==NULL)
{
return;
}
_inorderTraversal(root->left,arr,pi);
arr[(*pi)++]=root->val;
_inorderTraversal(root->right,arr,pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
if(root==NULL)
{
*returnSize=0;
return NULL;
}
*returnSize=BTSize(root);
int* arr=(int*)malloc(sizeof(int)*(*returnSize));
assert(arr);
int i=0;
_inorderTraversal(root,arr,&i);
return arr;
}