Grind 75 Python 做題記錄 104. Maximum Depth of Binary Tree
Grind 75 Python 做題記錄 104. Maximum Depth of Binary Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
# 觸底返 0
if not root: return 0
# 取左右深度最大值
max_depth_until_now = max(self.maxDepth(root.left),self.maxDepth(root.right))
# 回傳值加上當前 1 個深度
return max_depth_until_now + 1