Diameter of Binary Tree: Why LeetCode 543 Matters
Diameter of Binary Tree (#543) is one of the most elegant Easy problems on LeetCode — and it teaches a pattern that shows up repeatedly in Medium and Hard tree problems. The core idea is deceptively simple: find the longest path between any two nodes in a binary tree.
What makes this problem special is not the difficulty but what it teaches you. The "return one value, track another" pattern you learn here is the exact same technique required for Binary Tree Maximum Path Sum (#124), one of the hardest and most frequently asked tree problems at top tech companies.
If you are preparing for coding interviews, mastering the diameter of binary tree LeetCode problem is one of the highest-leverage moves you can make. It is the gentle on-ramp to dual-value recursion, and once you see it, you will recognize it everywhere.
Understanding the Diameter of Binary Tree Problem
The diameter of a binary tree is defined as the length of the longest path between any two nodes. This path may or may not pass through the root. Crucially, the length is measured in edges, not nodes — so a path that visits four nodes has a length of three.
Given the root of a binary tree, you need to return the length of the diameter. For example, given the tree [1,2,3,4,5], the longest path is 4 -> 2 -> 1 -> 3 or 5 -> 2 -> 1 -> 3, both with length 3.
The key constraint that trips people up is that the longest path does not have to go through the root. In a skewed or unbalanced tree, the diameter might exist entirely within a subtree. This is why a naive approach of just computing leftHeight + rightHeight at the root is insufficient.
- Diameter = longest path between ANY two nodes in the tree
- Path length is counted in edges, not nodes
- The path does NOT have to pass through the root
- A single-node tree has diameter 0 (no edges)
Why This Problem Matters
Diameter of Binary Tree (#543) is one of the most-asked Easy tree problems — it's the gentle introduction to the dual-value recursion pattern that becomes essential for Hard tree problems
Key Insight: Diameter Equals Left Height Plus Right Height
Here is the breakthrough insight for the diameter of binary tree LeetCode problem: at every node, the longest path passing through that node is leftHeight + rightHeight. The global diameter is the maximum of this value across all nodes in the tree.
But here is the trick — when a node reports its height back to its parent, it can only contribute one branch. You cannot go down the left subtree and up through the right subtree when continuing upward. So the node returns 1 + max(leftHeight, rightHeight) to its parent.
This creates the dual-value recursion pattern. Your DFS function computes two things at every node: the diameter passing through this node (leftHeight + rightHeight, used to update a global max), and the height of this node (1 + max of children, returned to the parent). You return one value but track another.
Implementation: DFS Solution for Tree Diameter
The implementation of the diameter tree DFS solution is remarkably clean once you understand the dual-value pattern. You define a variable to track the global maximum diameter, then write a recursive DFS function that returns the height of each subtree.
At each node, you recursively compute the left height and right height. You update the global diameter as max(diameter, leftHeight + rightHeight). Then you return 1 + max(leftHeight, rightHeight) to the parent. The base case is a null node, which returns 0.
The time complexity is O(n) because you visit every node exactly once. The space complexity is O(h) where h is the height of the tree, due to the recursion stack. For a balanced tree this is O(log n), and for a completely skewed tree it is O(n).
- Initialize a global variable: let diameter = 0
- DFS base case: if node is null, return 0
- Recursive case: leftH = dfs(node.left), rightH = dfs(node.right)
- Update global: diameter = max(diameter, leftH + rightH)
- Return to parent: return 1 + max(leftH, rightH)
- Time: O(n) — visit every node once
- Space: O(h) — recursion stack depth equals tree height
Pro Tip
The DFS function returns HEIGHT to its parent but tracks DIAMETER globally — this "return one, track another" pattern is the same used in Binary Tree Maximum Path Sum (#124)
Visual Walkthrough: Diameter of Binary Tree Example
Let us walk through the classic example tree [1,2,3,4,5] step by step. Node 1 is the root, with left child 2 and right child 3. Node 2 has left child 4 and right child 5. Nodes 3, 4, and 5 are leaf nodes.
Start DFS at node 1. Go left to node 2. Go left to node 4. Node 4 is a leaf — leftH = 0, rightH = 0. Update diameter = max(0, 0+0) = 0. Return 1 + max(0,0) = 1. Back at node 2, go right to node 5. Same as node 4: return 1, diameter stays 0.
Now at node 2: leftH = 1 (from node 4), rightH = 1 (from node 5). Update diameter = max(0, 1+1) = 2. Return 1 + max(1,1) = 2. Back at node 1, go right to node 3. Node 3 is a leaf: return 1, diameter stays 2.
Finally at node 1: leftH = 2 (from node 2), rightH = 1 (from node 3). Update diameter = max(2, 2+1) = 3. Return 1 + max(2,1) = 3. The final diameter is 3, which corresponds to the path 4 -> 2 -> 1 -> 3 (or 5 -> 2 -> 1 -> 3).
Edge Cases: When the Diameter Bypasses the Root
The most important edge case to internalize is that the diameter may not pass through the root. Consider a tree where the root has a deep left subtree with wide branching, but a short right subtree. The longest path could be entirely within the left subtree.
For a single-node tree, the diameter is 0 — there are no edges. For a linear tree (every node has only one child, like a linked list), the diameter equals n-1 where n is the number of nodes. For a perfectly balanced tree of height h, the diameter is 2h.
Another subtle case: a tree like [1,2,3,4,5,null,null,6,7]. The longest path goes 6 -> 4 -> 2 -> 5, with length 3 — it does not even touch node 1 (the root) or node 3. This is why tracking a global maximum at every node is essential, not just computing the value at the root.
- Single node: diameter = 0
- Linear tree (linked list shape): diameter = n - 1
- Balanced tree of height h: diameter = 2h
- Diameter may exist entirely in a subtree, bypassing root
Common Mistake
The diameter may NOT pass through the root — for [1,2,3,4,5,null,null,6,7], the longest path goes 6->4->2->5, bypassing root entirely. Always track global max, don't just return root's left+right.
What Diameter of Binary Tree Teaches: Dual-Value Recursion
The dual-value recursion pattern from this problem is one of the most reusable techniques in tree problems. The idea — your recursive function returns one value to its caller but tracks a different value globally — appears in several important LeetCode problems.
Binary Tree Maximum Path Sum (#124) uses the exact same structure. Your DFS returns the maximum single-branch sum to the parent, but tracks the maximum path sum (which can include both branches) as a global variable. Once you master the diameter of binary tree LeetCode pattern, #124 becomes a straightforward extension.
Other problems that use variants of this pattern include Longest Univalue Path (#687), Binary Tree Maximum Path Sum (#124), and path sum problems in general. Recognizing this "return one, track another" approach will save you significant time in interviews.
Practice the diameter of binary tree problem until the pattern is automatic, then move on to #124. YeetCode flashcards can help you drill both problems with spaced repetition so the dual-value recursion pattern stays fresh when you need it most.