HOT 100 › 索引 › B8 二叉树 Binary Tree
LC 104二叉树的最大深度Maximum Depth of Binary Tree 简单
求二叉树根到最远叶子的最大深度(路径节点数)。
思路 递归:深度 = 1 + max(左深, 右深),空树为 0。时间 O(n)。
class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: if not root: return 0 left = self.maxDepth(root.left) right = self.maxDepth(root.right) return 1 + max(left, right)