44. Maximum Depth of Binary Tree

44. Maximum Depth of Binary Tree

Easy
64.0%
2.4k
dfsbfstreebinary-treerecursion
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large-scale system represents hierarchical structures such as organization charts, feature dependencies, decision trees, or file systems using binary trees.

To estimate processing complexity, memory usage, or recursion limits, the system often needs to compute how deep the tree goes. This depth determines:

  • worst-case traversal cost
  • recursion stack size
  • tree balance indicators
  • performance characteristics

The maximum depth of a binary tree is defined as the number of nodes along the longest path from the root node down to the farthest leaf node.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given the root of a binary tree, return its maximum depth.

Input Format

  • root: the root node of a binary tree

Each node is defined as:

TreeNode { int val; TreeNode left; TreeNode right; }

Output Format

  • A single integer representing the maximum depth of the binary tree

Examples

Example 1:

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

Example 2:

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

Example 3:

Input: root = []
Output: 0

Constraints

  • 0 <= number of nodes <= 10,000
  • -10^9 <= Node.val <= 10^9

Loading editor...

[3,9,20,null,null,15,7]
3

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback