---恢复内容开始---
题目描述:
方法一:递归
class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return res.append(root.val) helper(root.left) helper(root.right) helper(root) return res
方法二:迭代
class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: res,stack = [],[root] if not root: return res while stack: node = stack.pop() res.append(node.val) if node.right: stack.append(node.right) if node.left: stack.append(node.left) return res
---恢复内容结束---