给定一个二叉树的根节点 root
,返回 它的 中序 遍历 。
输入:root = [1,null,2,3] 输出:[1,3,2]
示例 2:
输入:root = [] 输出:[]
示例 3:
输入:root = [1] 输出:[1]
提示:
- 树中节点数目在范围
[0, 100]
内 -100 <= Node.val <= 100
思路:
-
- 外层
while
循环控制遍历条件,继续遍历直到root
为NULL
且栈为空(top > 0
)。 - 内层
while (root != NULL)
负责向左遍历,直到到达最左侧的叶子节点:stk[top++] = root;
:将当前节点入栈,并将top
增加。root = root->left;
:继续遍历左子树。
- 没有左子树时,取出栈顶的节点:
root = stk[--top];
:弹出栈顶节点。res[(*returnSize)++] = root->val;
:将当前节点的值加入结果数组,并增大返回大小。
- 继续遍历右子树:
root = root->right;
:将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().
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 501);
*returnSize = 0;
struct TreeNode* predecessor = NULL;
while (root != NULL) {
if (root->left != NULL) {
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
predecessor = root->left;
while (predecessor->right != NULL && predecessor->right != root) {
predecessor = predecessor->right;
}
// 让 predecessor 的右指针指向 root,继续遍历左子树
if (predecessor->right == NULL) {
predecessor->right = root;
root = root->left;
}
// 说明左子树已经访问完了,我们需要断开链接
else {
res[(*returnSize)++] = root->val;
predecessor->right = NULL;
root = root->right;
}
}
// 如果没有左孩子,则直接访问右孩子
else {
res[(*returnSize)++] = root->val;
root = root->right;
}
}
return res;
}