- 문제 링크: https://leetcode.com/problems/maximum-depth-of-binary-tree/
- 난이도: Easy
binary tree의 max depth를 구하는 문제
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
def bt(curr_depth: int, node: TreeNode) -> int:
# leaf
if not node.left and not node.right:
return curr_depth
max_depth = 0
if node.left:
max_depth = max(max_depth, bt(curr_depth+1, node.left))
if node.right:
max_depth = max(max_depth, bt(curr_depth+1, node.right))
return max_depth
return bt(1, root)
반응형
'Online Judge > LeetCode' 카테고리의 다른 글
[LeetCode][Python] 90. Subsets II (0) | 2024.09.17 |
---|---|
[LeetCode][Python] 543. Diameter of Binary Tree (0) | 2024.09.17 |
[LeetCode][Python] 78. Subsets (0) | 2024.09.16 |
[LeetCode][Python] 226. Invert Binary Tree (0) | 2024.09.16 |
[LeetCode][Python] 19. Remove Nth Node From End of List (0) | 2024.09.13 |