Browse curriculum

Maximum Depth of Binary Tree (LeetCode 104)

Maximum Depth of Binary Tree (LeetCode 104): find the maximum depth (height) of a binary tree.

Examples

Input:root = [3,9,20,null,null,15,7]
Output:3
Explain:

The tree has nodes 3 -> (9, 20). 20 -> (15, 7). The longest path is 3->20->15 (length 3).

Input:root = [1,null,2]
Output:2

Problem Understanding

Return the maximum depth of the binary tree under root: how many nodes lie on the longest downward path from the root to any leaf, counting both the root and the leaf.

Algorithm Strategy (Bottom-Up)

We use Post-order DFS (Bottom-Up) to solve this.

  1. Base Case: If the node is null, its depth is 0.
  2. Recursive Step:
    • Ask the Left Child: "What is your max depth?"
    • Ask the Right Child: "What is your max depth?"
  3. Calculation: My Depth = max(left_depth, right_depth) + 1.

Interactive Visualization

→ N = THE DEPTH A NODE RETURNED

CALL

Stack depth

1

Max depth

:

call stack

maxDepth(3)

 

maxDepth(3)

Next

Ask 3's left child for its depth

Free preview: the first 9 of 27 steps

Tree

Each node asks its left child, then its right child, for a depth, and returns the larger plus one. A node's returned depth appears under it only when it returns, so the numbers fill in from the leaves up; an empty child shows the 0 it returns.

Dry Run

Node Left Depth Right Depth Calculation Return Value
9 0 0 max(0,0) + 1 1
15 0 0 max(0,0) + 1 1
7 0 0 max(0,0) + 1 1
20 1 (from 15) 1 (from 7) max(1,1) + 1 2
3 1 (from 9) 2 (from 20) max(1,2) + 1 3

Edge Cases

  • Empty Tree (Root is null): Return 0.
  • Single Node: Return 1.
  • Skewed Tree: Effectively a linked list, depth = N.

Solution

Bottom-up in every language: ask the left side, ask the right side, return the larger + 1.

Maximum Depth of Binary Tree Code
1var maxDepth = function(root) {
2 if (!root) return 0;
3 const left = maxDepth(root.left);
4 const right = maxDepth(root.right);
5 return Math.max(left, right) + 1;
6};

Maximum Depth of Binary Tree in Python, Java, C++, TypeScript and C

Maximum Depth of Binary Tree in Python
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        left = self.maxDepth(root.left)
        right = self.maxDepth(root.right)
        return max(left, right) + 1
Maximum Depth of Binary Tree in Java
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    }
}
Maximum Depth of Binary Tree in C++
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) return 0;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return max(left, right) + 1;
    }
};
Maximum Depth of Binary Tree in TypeScript
function maxDepth(root: TreeNode | null): number {
    if (!root) return 0;
    const left = maxDepth(root.left);
    const right = maxDepth(root.right);
    return Math.max(left, right) + 1;
}
Maximum Depth of Binary Tree in C
int maxDepth(struct TreeNode* root) {
    if (root == NULL) return 0;
    int left = maxDepth(root->left);
    int right = maxDepth(root->right);
    return (left > right ? left : right) + 1;
}

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Handle the empty node

    If the node is empty, return 0.

  2. Ask both children

    Compute the depth of the left subtree and of the right subtree.

  3. Return one more than the deeper side

    Return max(left, right) + 1.

Frequently asked questions

How do you find the maximum depth of a binary tree?

Recursively: an empty node has depth 0, and any other node has depth max(left depth, right depth) + 1. The value returned by the root is the answer.


What is the time and space complexity?

O(n) time, since every node is visited once. The recursion uses O(h) stack space, where h is the height: O(log n) for a balanced tree and O(n) for a chain.


What is the depth of an empty tree?

It is 0. The base case returns 0 for an empty node, so an empty root returns 0 directly.

New to the technique behind this problem? Watch it run step by step:

Depth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description