class Solution:
def inorderTraversal(self, root: TreeNode):
if not root:
return []
stk = []
res = []
cur = root
while stk or cur:
while cur:
stk.append(cur)
cur = cur.left
cur = stk.pop()
res.append(cur.val)
cur = cur.right
return res
leetcode 94. 二叉树的中序遍历
最新推荐文章于 2025-08-18 11:17:04 发布
本文介绍了一种使用栈实现二叉树中序遍历的算法。通过迭代而非递归的方式,该方法有效地遍历了二叉树的节点,先访问左子树,然后访问根节点,最后访问右子树。
1213

被折叠的 条评论
为什么被折叠?



