摘要:
216.组合总和III class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: res = [] self.tracebacking(n, k, 1, 0, [], res) return res d 阅读全文
摘要:
第77题. 组合 需要注意剪枝细节 纵向代表递归,横行代表取数 1、回溯 class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] self.backtrack(n, k, 1, [], res) r 阅读全文
摘要:
669. 修剪二叉搜索树 树的修剪方式赋值。 1、递归法 class Solution: def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: if root is None: 阅读全文
摘要:
235. 二叉搜索树的最近公共祖先 关键点:最近公共祖先的判断,二叉树的特性 1、做二叉树的模式 class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'Tr 阅读全文
摘要:
二叉搜素树,利用中序遍历的升序结果 530.二叉搜索树的最小绝对差 1、递归中序遍历 class Solution: def __init__(self): self.pre = None self.res = float('inf') def getMinimumDifference(self, 阅读全文
摘要:
654.最大二叉树 1、使用切片 class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: if len(nums) == 0: return None max_val = 阅读全文
摘要:
513.找树左下角的值 1、层序遍历迭代法 def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: queue = [root] res = float('-inf') while queue: n = len(queue) f 阅读全文
摘要:
110.平衡二叉树 1、递归法 class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: if self.get_height(root) != -1: # -1 代表高度差大于 1 return True els 阅读全文
摘要:
104.二叉树的最大深度 1、后续遍历递归法 class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if root is None: return 0 left_depth = self.maxDepth(root. 阅读全文
摘要:
前序遍历 统一写法用None 来区分遍历查找的节点和处理节点 1、递归法 class Solution: def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: res = [] self.preorder(root, 阅读全文