递归法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int inorder(struct TreeNode *root,int *arrRet,int *returnSize){
if(root){
inorder(root->left,arrRet,returnSize);
arrRet[(*returnSize)++] = root->val;
inorder(root->right,arrRet,returnSize);
}
return 0;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int *arrRet = (int*)malloc(sizeof(int)*200);
*returnSize = 0;
inorder(root,arrRet,returnSize);
return arrRet;
}
非递归
网友评论