视频学习记录
https://www.bilibili.com/video/BV1UD4y1Y769/
例题和课后作业代码记录 104. 二叉树的最大深度
https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution : def maxDepth (self, root: Optional [TreeNode] ) -> int : if not root: return 0 return max (self .maxDepth(root.left), self .maxDepth(root.right)) + 1
111. 二叉树的最小深度
https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution : def minDepth (self, root: Optional [TreeNode] ) -> int : if not root: return 0 if not root.left and not root.right: return 1 if not root.left: return self .minDepth(root.right)+1 elif not root.right: return self .minDepth(root.left)+1 else : return min (self .minDepth(root.left), self .minDepth(root.right)) + 1
404. 左叶子之和
https://leetcode.cn/problems/sum-of-left-leaves/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution : def sumOfLeftLeaves (self, root: Optional [TreeNode] ) -> int : if not root: return 0 if root.left and not root.left.left and not root.left.right: return root.left.val + self .sumOfLeftLeaves(root.right) return self .sumOfLeftLeaves(root.left) + self .sumOfLeftLeaves(root.right)
112. 路径总和
https://leetcode.cn/problems/path-sum/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution : def hasPathSum (self, root: Optional [TreeNode], targetSum: int ) -> bool : if not root: return False targetSum -= root.val if targetSum == 0 and not root.left and not root.right: return True return self .hasPathSum(root.left, targetSum) or self .hasPathSum(root.right, targetSum)
129. 求根节点到叶节点数字之和
https://leetcode.cn/problems/sum-root-to-leaf-numbers/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Solution : def sumNumbers (self, root: Optional [TreeNode] ) -> int : def dfs (root: Optional [TreeNode], carry:int ): carry *= 10 carry += root.val if not root.left and not root.right: return carry elif not root.left: return dfs(root.right, carry) elif not root.right: return dfs(root.left, carry) else : return dfs(root.left, carry) + dfs(root.right, carry) return dfs(root, 0 )
1448. 统计二叉树中好节点的数目
https://leetcode.cn/problems/count-good-nodes-in-binary-tree/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Solution : def goodNodes (self, root: Optional [TreeNode] ) -> int : ans = 0 def dfs (root: Optional [TreeNode], rec_max:int ): if not root: return if rec_max <= root.val: nonlocal ans ans += 1 rec_max = root.val dfs(root.left, rec_max) dfs(root.right, rec_max) dfs(root, -1e5 ) return ans
987. 二叉树的垂序遍历
https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Solution : def verticalTraversal (self, root: Optional [TreeNode] ) -> List [List [int ]]: group = defaultdict(list ) min_col = 0 def dfs (root, row, col ): if not root: return nonlocal min_col if min_col > col: min_col = col group[col].append((row, root.val)) dfs(root.left, row+1 , col-1 ) dfs(root.right, row+1 , col+1 ) dfs(root, 0 , 0 ) ans = [] for i in range (min_col, min_col+len (group)): g = group[i] g.sort() ans.append([v for _, v in group[i]]) return ans