前序遍历
中序遍历
int* _inorderTraversal(struct TreeNode*root,int *a,int* returnSize)
{
if(root==NULL)
{
return;
}
_inorderTraversal(root->left,a,returnSize);
a[(*returnSize)++]=root->val;
_inorderTraversal(root->right,a,returnSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* a=(int*)malloc(sizeof(int)*1000);//1000随意给的
if(a==NULL)
{
printf("malloc fail");
exit;
}
*returnSize=0;
_inorderTraversal(root,a,returnSize);
return a;
}
后序遍历
void _postorderTraversal(struct TreeNode*root,int*a,int*returnSize)
{
if(root==NULL)
{
return NULL;
}
_postorderTraversal(root->left,a,returnSize);
_postorderTraversal(root->right,a,returnSize);
a[(*returnSize)++]=root->val;
}
int size(struct TreeNode*root)
{
if(root==NULL)
{
return NULL;
}
return size(root->left)+size(root->right)+1;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize){
int k=size(root);
int* a=(int *)malloc(sizeof(int)*k);
if(a==NULL)
{
printf("fail malloc");
}
*returnSize=0;
_postorderTraversal(root,a,returnSize);
return a;
}