Skip to content
Learn Netverks
Company prep HCLTech
Junior (1–3 years) Coding / DSA Easy

Find the maximum depth of a binary tree

Reported in HCLTech interview loops. Tree recursion warm-up that often leads to balanced-tree and diameter follow-ups.

Role
SDE
Location
Remote

Often asked in HCLTech technical or coding rounds. Prepare a clear spoken answer plus key trade-offs.

Try answering aloud first

Cover trade-offs, structure, and a concrete example before revealing the baseline response.

Spoiler-free prep mode

How to frame this at HCLTech: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.

Max depth is 1 + max(depth(left), depth(right)) with base case null → 0. This is a post-order computation: you need child depths before combining at the parent.

function maxDepth(root) {
  if (!root) return 0;
  return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}

Complexity: O(n) time visiting each node once; O(h) recursion stack where h is height (O(n) worst case for skewed tree).

Iterative BFS counts levels by processing queue size per level—useful when stack depth is a concern. Follow-ups: minimum depth, check if balanced (compare left/right heights at each node), tree diameter, and serialize/deserialize.

Comments (0)

Share how this question came up in your loop, or add tips for others preparing.

Log in to comment on this question.