Diameter of Binary Tree
Each node returns the number of nodes on its longest chain going down. While it combines its two children's answers, it also checks the path that bends at this node: left chain + right chain. The largest value seen anywhere is the answer. Every node is visited once.
Every path has one highest node, and its length is the deepest left chain plus the deepest right chain of that node, counted in edges.
Problem
You are given the root of a binary tree. Find its diameter: the number of edges on the longest path between any two nodes.
The path does not have to pass through the root. It can go up from one node and down into another part of the tree, or it can stay on one side of the root.
The root of a binary tree. It has between 1 and 10^4 nodes.
One number: the number of edges on the longest path.
The longest path is 4 → 2 → 1 → 3 (or 5 → 2 → 1 → 3). It has 3 edges and passes through the root.
There is only one edge, so the longest path is 1 → 2.
The longest path is 6 → 4 → 2 → 5 → 7. It has 4 edges and does not touch the root.
- • The number of nodes is in the range [1, 10^4].
- • −100 ≤ Node.val ≤ 100
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
A path can start and end at any two nodes. Does it always have to pass through the root?
- 2.
Every path has a highest node, where it turns around. If I pick one node, what two pieces of the tree does a path through it join?
- 3.
How long is the longest straight chain going down from one child? Can I get that for each child first?
- 4.
The question asks for edges. How many edges does a path with one node have?
- 5.
If the left and right children each report their chain length, can this node work out its own best path without looking at the tree again?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | At each node, measure the left and right chains with fresh searches, then add them | O(N²) | O(H) |
| Optimal | One post-order pass: each node returns its height and records the path that bends at it | O(N) | O(H) |
Why does this belong to this pattern?
- “longest path between any two nodes”The path can bend at any node, so every node must be checked, not just the root.
- “may or may not pass through the root”This is not a root-to-leaf question. The answer depends on the best bend point.
- “number of edges”Count the edges between nodes. A single node has diameter 0.
Each node returns one number to its parent: its height. The answer is different: it can bend at any node, so it is kept in best and updated while combining. This is the pattern in one sentence: return one summary upward, and record the whole-tree answer on the way. The next question, Balanced Binary Tree, uses the same post-order shape but returns a yes/no flag.
Don't make this mistake
On [1, 2, null, 4, 5, 6, null, null, 7] the root has one side of height 3 and an empty right side, so this returns 3. The real answer is 4: the path 6 → 4 → 2 → 5 → 7 bends at node 2 and never touches the root. Diameter is a maximum over all nodes.
The question counts edges. On [1, 2, 3] the path 2 → 1 → 3 has 2 edges, but the wrong line gives 3. On the single node [1] it gives 1 instead of 0. lh and rh already count the edges from this node down each side, so do not add 1.
A parent can extend only one side of a node. On [1, 2, 3, 4, 5, 6, null, null, 7], node 2 would return 5 instead of 3. The root would then add its other side and report 6 edges. No path in this 7-node tree has 6 edges. Both sides count only at the bend, never on the way up.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.