[c语言算法]二叉树中序遍历
2019-08-14 本文已影响0人
Ucan先生
递归法
/**
* 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;
}
非递归