题目:原题链接(中等)
标签:树、二叉树、二叉树-遍历、栈
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | 32ms (96.42%) | ||
Ans 2 (Python) | 28ms (99.19%) | ||
Ans 3 (Python) |
LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(递归):
def inorderTraversal(self, root: TreeNode) -> List[int]:
if root:
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
else:
return []
解法二(迭代):
def inorderTraversal(self, root: TreeNode) -> List[int]:
# 处理特殊情况
if not root:
return []
stack = [root]
ans = []
while stack:
if now := stack[-1]:
stack.append(now.left)
else:
stack.pop()
if not stack:
break
now = stack.pop()
ans.append(now.val)
stack.append(now.right)
return ans