Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

DSA›Trees›Maximum Depth of Binary Tree
EasyTrees

Maximum Depth of Binary Tree

treerecursiondfs

Problem

Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root node down to the farthest leaf node.

Examples

Example 1

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

Output: 3

Constraints

  • •The number of nodes is in the range [0, 10^4].

Hints

Hint 1

Recursion: depth = 1 + max(depth(left), depth(right))

Solutions

public int maxDepth(TreeNode root) {
    if (root == null) return 0;
    return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
Java

Time: O(n) · Space: O(h)