FastPrepMaximum Depth of a Binary Tree
Problems/Maximum Depth of a Binary Tree

Maximum Depth of a Binary Tree

AMD logoAMD● EasyNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source asks for maximum depth of aBST. The same depth computation applies to its binary-tree shape; this portable practice does not grade BST insertion or key ordering. Node-count convention, serialization, bounds and interface are authored; approximate source match90%.

Return the maximum number of nodes on a path from the root to a leaf. An empty tree has depth0 and a single node has depth1. Node values do not affect depth.

Function

maxDepth(root: TreeNode) → int

Examples

Example 1

root = [3,1,5,null,2,4,6]return = 3

Every longest root-to-leaf path includes3nodes, such as3 → 1 → 2.

Example 2

root = []return = 0

There is no node in the empty tree.

Constraints

  • 0 <= serialized tree length <= 400.
  • -1000 <= every non-null node value <= 1000.
  • The input is a valid acyclic binary tree serialized level by level, with null for a missing child; [] denotes an empty tree.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int maxDepth(TreeNode root) {
    // Write your code here.
}
Checking account…