classSolution:definorderTraversal(self, root: TreeNode)-> List[int]:ifnot root:return[]
res =[]if root.left:
res += self.inorderTraversal(root.left)
res.append(root.val)if root.right:
res += self.inorderTraversal(root.right)return res
S2:迭代
classSolution:definorderTraversal(self, root: TreeNode)-> List[int]:ifnot root:return[]
res =[]
stack =[root]while root.left:# 一路往左直到最左端,并将其逐个压入堆栈
stack.append(root.left)
root = root.left
while stack:
tmp = stack.pop()# 弹出并将访问其值
res.append(tmp.val)if tmp.right:# 若存在有节点则将其压入堆栈
stack.append(tmp.right)
tmp = tmp.right
while tmp.left:# 一路往左直到最左端,并将其逐个压入堆栈
stack.append(tmp.left)
tmp = tmp.left
return res