Tree 发表于 2020-03-11 | 分类于 LeetCode | 汇总树的题目 123456# Definition for a binary tree node.class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None 94. 中序遍历12345678910111213141516class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root else []class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: res, stack = [], [] while True: while root: stack.append(root) root = root.left if not stack: return res node = stack.pop() res.append(node.val) root = node.right 144. 前序遍历123456789101112131415161718class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root else []class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: res, stack = [], [] if not root: return res stack.append(root) 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 145. 后序遍历1234567891011121314151617181920class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root else []class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: res, stack = [], [] if not root: return res stack.append(root) while stack: node = stack.pop() res.append(node.val) if node.left: stack.append(node.left) if node.right: stack.append(node.right) return res[::-1]