Friedrich Ewald My Personal Website

Leetcode: Maximum Depth of Binary Tree

Calculate the maximum depth of a binary tree. This is best/easiest solved with recursion.

# 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:
    if root is None:
      return 0
    return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))


About the author

is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.