94. 二叉树的中序遍历
给定一个二叉树的根节点 root ,返回它的 中序 遍历。
代码分析:
/**
* 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().
*/
void inorder(struct TreeNode* root, int* res, int* resSize) {
if (!root) { //树是空
return;
}
inorder(root->left, res, resSize);
//相当于传进去的returnSize指针,取它的内容++ 作为res数组下标,然后存入访问到的根节点
res[(*resSize)++] = root->val;
inorder(root->right, res, resSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 501);
*returnSize = 0; //returnSize指针所指内存内容为0
inorder(root, res, returnSize);
return res; //返回res所指向内存空间的所有
}