104. Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.


Note: There is an idea of tail-recursion, which is useful for C++
Is it also good in Python?


def maxDepth(root: Optional[TreeNode]):
       
    if root is None:
        return 0
   
    left_height  = maxDepth(root.left)
    right_height = maxDepth(root.right)
   
    return max(left_height, right_height) + 1

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break