Diameter of Binary Tree
Find the longest path between any two nodes, counted in edges, by tracking heights on the way up.
The problem
You get the root of a binary tree. Return the length of its diameter: the number of edges on the longest path between any two nodes. That path does not have to pass through the root.
Input root = [1, 2, 3, 4, 5, null, 6]
Output 4
The path 4, 2, 1, 3, 6 has 4 edges.
Input root = [9, 4, null, 2, 6, 1, null, null, 7]
Output 4
The longest path is 1, 2, 4, 6, 7. It bends at 4 and never reaches the root 9.
Key constraint: The longest path can bend at any node, not only at the root, so every node is a possible turning point.
How to spot the pattern
- The answer is a path that can bend at any node of the tree.
- The best path bending at a node depends only on the heights of its two subtrees.
The idea
Every path has one highest node where it bends: it goes down on the left and down on the right from there. The longest path bending at a node has height(left) + height(right) edges, where an empty side has height 0. One post-order DFS can return each subtree's height to its parent and, at the same time, keep the best left + right seen anywhere.
Brute force: At every node, compute the heights of both children with a separate height function, which repeats the same work and takes O(n^2) time on a skewed tree.
Step by step
- 1Write a helper that returns the height of a subtree in nodes, with 0 for an empty one.
- 2In the helper, get the left and right heights first.
- 3Update the best answer with
left + right, the longest path that bends at this node. - 4Return
1 + max(left, right)to the parent. - 5Call the helper on the root and return the best value seen.
See the pattern move
The tree dfs and bst pattern on a small example. Step through it, or press play.
Code
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
best = 0
def height(node: Optional[TreeNode]) -> int:
nonlocal best
if node is None:
return 0
left = height(node.left)
right = height(node.right)
best = max(best, left + right) # longest path that bends at this node
return 1 + max(left, right)
height(root)
return bestComplexity
Time
O(n)
Space
O(h)
One post-order pass visits each node once; the recursion stack is as deep as the tree's height h.
Watch out for
- Returning
left + rightfrom the helper: the parent needs the height of one branch, not the bent path. - Checking only the path through the root, which misses a longer path that bends lower down.
- Mixing up nodes and edges: a single node has diameter 0.
Follow-up questions
What if every edge had a weight?
Return the heaviest downward path instead of the height, adding the edge's weight when you go up one level; the update at each node is still left + right.
How do you find the diameter of a tree that is not binary?
Keep the two largest child heights at each node. Another way: BFS from any node to the farthest node, then BFS again from that node; the second distance is the diameter.
In Hinglish
Har node par socho ki path yahin mud raha hai: left height plus right height. Isse best update karo, lekin parent ko sirf ek taraf ki height bhejo.