Maximum Depth of Binary Tree

EASY

Description

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, 104].
  • -100 <= Node.val <= 100

Approaches

Checkout 2 different approaches to solve Maximum Depth of Binary Tree. Click on different approaches to view the approach and algorithm in detail.

Recursive Depth-First Search (DFS)

This is the most intuitive and common approach. It leverages the recursive nature of a tree. The maximum depth of a tree is defined as 1 (for the root) plus the maximum of the depths of its left and right subtrees.

Algorithm

  • If the root node is null, return 0.
  • Calculate the depth of the left subtree by making a recursive call: leftDepth = maxDepth(root.left).
  • Calculate the depth of the right subtree by making a recursive call: rightDepth = maxDepth(root.right).
  • Return 1 + max(leftDepth, rightDepth).

The algorithm works by defining a function that calculates the depth of a subtree rooted at a given node.

  • Base Case: If the current node is null, it represents an empty tree or the end of a path, which has a depth of 0.
  • Recursive Step: For a non-null node, we recursively call the function on its left and right children to find the depths of the left and right subtrees (leftDepth and rightDepth).
  • The depth of the tree rooted at the current node is then 1 + Math.max(leftDepth, rightDepth). The +1 accounts for the current node itself. The process starts by calling this function with the root of the entire tree.
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        
        return Math.max(leftDepth, rightDepth) + 1;
    }
}

Complexity Analysis

Time Complexity: O(N)Space Complexity: O(H)

Pros and Cons

Pros:
  • The code is very concise, elegant, and easy to understand.
  • It directly models the recursive definition of a tree's depth.
Cons:
  • For extremely deep trees, it might lead to a StackOverflowError due to deep recursion.
  • Can be less space-efficient than BFS for very skewed trees where the height approaches the number of nodes.

Code Solutions

Checking out 4 solutions in different languages for Maximum Depth of Binary Tree. Click on different languages to view the code.


Video Solution

Watch the video walkthrough for Maximum Depth of Binary Tree



Algorithms:

Depth-First SearchBreadth-First Search

Data Structures:

TreeBinary Tree

Companies:

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.