diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 2294c1d9..c4d94d4d 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -452,14 +452,17 @@ class Solution: 递归法: ```python -class solution: - def maxdepth(self, root: 'node') -> int: +class Solution: + def maxDepth(self, root: 'Node') -> int: if not root: return 0 - depth = 0 - for i in range(len(root.children)): - depth = max(depth, self.maxdepth(root.children[i])) - return depth + 1 + + max_depth = 1 + + for child in root.children: + max_depth = max(max_depth, self.maxDepth(child) + 1) + + return max_depth ``` 迭代法: