leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

maximum-depth.py (485B)


      1 # Definition for a binary tree node.
      2 # class TreeNode:
      3 #     def __init__(self, val=0, left=None, right=None):
      4 #         self.val = val
      5 #         self.left = left
      6 #         self.right = right
      7 class Solution:
      8     def maxDepth(self, root: Optional[TreeNode]) -> int:
      9         return dfs(root, 0)
     10 
     11 
     12 def dfs(current, depth) -> int:
     13 
     14     if current is None:
     15         return depth
     16 
     17     left = dfs(current.left, depth + 1)
     18     right = dfs(current.right, depth + 1)
     19 
     20     return max(left, right)