543. Diameter of Binary Tree
easyAsked at MetaReturn the length of the longest path between any two nodes in a binary tree. Meta asks this to test whether you grasp the dual-return-vs-side-effect pattern in tree recursion — same shape as Binary Tree Maximum Path Sum but easier numbers.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Meta loops.
- Glassdoor (2026-Q1)— Meta E3/E4 phone-screen reports cite this as a recurring tree recursion problem.
- Blind (2025-10)— Recurring Meta interview question.
Problem
Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. The length of a path between two nodes is represented by the number of edges between them.
Constraints
The number of nodes in the tree is in the range [1, 10^4].-100 <= Node.val <= 100
Examples
Example 1
root = [1,2,3,4,5]3Explanation: Diameter is the path 4 -> 2 -> 1 -> 3 (or 5 -> 2 -> 1 -> 3), length 3 edges.
Example 2
root = [1,2]1Approaches
1. DFS with depth + side-effected best (optimal)
Recurse to compute depth at each node. Side-effect a global with leftDepth + rightDepth (which is the diameter passing through this node).
- Time
- O(n)
- Space
- O(h) recursion
function diameterOfBinaryTree(root) {
let best = 0;
function depth(node) {
if (!node) return 0;
const l = depth(node.left);
const r = depth(node.right);
best = Math.max(best, l + r);
return 1 + Math.max(l, r);
}
depth(root);
return best;
}Tradeoff: Single linear pass. The function RETURNS depth (used by the parent) but SIDE-EFFECTS the diameter through this node into a global. Same dual-purpose pattern as Maximum Path Sum.
Meta-specific tips
Meta interviewers grade this on the dual-purpose recursion pattern. State out loud: 'depth(node) returns the max depth from this node downward — used by the parent. At every node, I also UPDATE the global with leftDepth + rightDepth, which is the diameter passing through THIS node.' That separation is what makes the code work without a quadratic blowup.
Common mistakes
- Computing diameter as depth(root) — only correct if the diameter passes through root, which isn't always the case.
- Returning leftDepth + rightDepth from depth() — breaks the parent's calculation.
- Forgetting that diameter counts EDGES, not nodes (so a single node has diameter 0).
Follow-up questions
An interviewer at Meta may pivot to one of these next:
- Diameter of N-ary Tree (LC 1522) — track top 2 children's depths.
- Binary Tree Maximum Path Sum (LC 124) — same pattern with values.
- What if the tree had weighted edges?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why update global with l + r and return 1 + max(l, r)?
A path through this node uses BOTH children. A path extending through this node to a parent can only use ONE child. So the global is l+r; the return is 1 + max(l, r).
What does +1 mean in the return?
The edge from this node to its deepest descendant. Depth is measured in edges; this node contributes one edge to whichever side is taller.
Practice these live with InterviewChamp.AI
Drill Diameter of Binary Tree and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →