Advertisement

Maximum Depth of Binary Tree - LeetCode 104 Solution

Maximum Depth of Binary Tree - Complete Solution Guide

Maximum Depth of Binary Tree is LeetCode problem 104, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Constraints: The number of nodes in the tree is in the range [0, 10 4 ] . -100 <= Node.val <= 100

Detailed Explanation

You're tasked with finding the 'maximum depth' of a given binary tree. It might sound straightforward, but let's be precise about what "maximum depth" means here. It's defined as the number of nodes you'd traverse along the *longest path* starting from the `root` node and ending at the *farthest leaf* node. Crucially, we're counting *nodes*, not edges. So, an empty tree has a depth of 0, and a tree with just a single root node has a depth of 1. This distinction is vital for correctly interpreting the problem statement.

Solution Approach

The provided solution leverages a recursive Depth-First Search (DFS) approach, which is wonderfully intuitive for problems involving tree depths. The fundamental idea is that the maximum depth of any tree (or subtree) is determined by its deepest child branch. For a given `root` node, its maximum depth is `1` (for itself) plus the maximum depth found in either its left or right subtree. The algorithm works like this: First, it establishes a crucial base case: if the `root` is `None` (meaning an empty tree or we've traversed past a leaf node), its depth is `0`. This handles both an empty input tree and the termination condition for recursive calls. Otherwise, if there's a node, the function recursively calls itself on the `root.left` and `root.right` children to find their respective maximum depths. Once those values (`left_depth` and `right_depth`) return, the current node simply takes the maximum of these two depths and adds `1` (to account for itself) before returning. This elegant recursive decomposition perfectly mirrors the definition of maximum depth.

Step-by-Step Algorithm

  1. Step 1: Check if the root node is null. If it is, return 0 (base case: empty tree).
  2. Step 2: Recursively call `maxDepth` on the left subtree to get its depth (`leftDepth`).
  3. Step 3: Recursively call `maxDepth` on the right subtree to get its depth (`rightDepth`).
  4. Step 4: Return the maximum of `leftDepth` and `rightDepth`, plus 1 (to account for the current node).

Key Insights

  • **Recursive Definition for Depth:** The most critical insight is realizing that the depth of any non-empty node can be defined recursively: `Depth(node) = 1 + max(Depth(node.left), Depth(node.right))`. This precise mathematical relationship is the backbone of the efficient recursive solution, directly translating into code.
  • **Explicit Base Case for Null Nodes:** Handling the `if not root: return 0` case is more than just an edge case; it's the fundamental termination condition for the recursion. It correctly assigns a depth of zero to an empty branch, preventing infinite recursion and providing the 'ground truth' for depth calculations when a leaf node's children are queried.
  • **Modeling Tree Structure with Recursion:** This problem beautifully showcases how recursion naturally aligns with the hierarchical structure of a tree. Each recursive call essentially processes a subtree, asking the same question ("What's your maximum depth?") to its children, allowing the results to bubble up and consolidate at the root without complex iterative state management.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: Accenture, Adobe, Apple, Arista Networks, Bloomberg, Infosys, LinkedIn, Meta, Microsoft, Qualcomm, SAP, Spotify, Uber, Yahoo, Yandex.