DSA Tracker

Problem

Pattern this teaches:Tree DFS

Easy

Diameter of Binary Tree

An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
3
Core for
4 roles

The problem

Among every pair of nodes in a binary tree, find the pair that are farthest apart and return how many edges separate them. That longest path does not have to pass through the root.

Example 1

Input
[8,4,9,2,6,null,10,1,null,null,7]
Output
5
Why
The longest path 1-2-4-8-9-10 crosses 5 edges.

Example 2

Input
[1,2]
Output
1
Why
Only path 1-2 has 1 edge.

Example 3

Input
[1,null,2,3]
Output
2
Why
The longest path is 3 -> 2 -> 1, with 2 edges.

Constraints

  • The number of nodes is in the range [1, 104].
  • -100 <= Node.val <= 100

How to think about it

Updated 2026-09-29

Every path in a binary tree has a unique highest node where it turns around. The longest path curving through any specific node is the maximum depth of its left branch plus the maximum depth of its right branch. Track the global maximum of this sum while bubbling depth upwards.

Approaches, worst first

  1. Separate height calls at every node

    time O(n^2) · space O(h)

    For each node in the tree, invoke a height function on left and right subtrees and compute `leftHeight + rightHeight`. Recurse into children. Repeatedly visits nodes across multiple height calculations.

  2. Postorder bottom-up aggregationWrite this one

    time O(n) · space O(h)

    Bottom-up DFS returns the depth of the current subtree. Along the way, update a global diameter tracker with `leftDepth + rightDepth`. Returns tree depth to the parent in single-pass linear time.

Where people lose marks · 3
  • Assuming the longest path must pass through the root node; a deep, lopsided subtree can contain the entire diameter.
  • Counting nodes instead of edges; a path across 4 nodes contains 3 edges.
  • Returning the diameter from the recursive helper instead of the subtree height, which corrupts parent depth calculations.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .