InterviewHubPRO
Section Overview
Q54

Question 54 of 100

Part 7: Binary Trees, BSTs & Traversals (53 - 64)

54%
DSA Patterns (Multi-Lang)•Part 7: Binary Trees, BSTs & Traversals (53 - 64)

Maximum Depth of Binary Tree.

In Simple Words: Height of a tree is 1 + max(depth(left), depth(right)). Base case: empty node returns 0.

  • Time: $O(N)$ | Space: $O(H)$
Multi-Language Solutions
Switch tabs or choose All
def maxDepth(root: TreeNode | None) -> int:
    if not root: return 0
    return 1 + max(maxDepth(root.left), maxDepth(root.right))