Home
DSA Patterns
Depth-First Search
Diameter of Binary Tree
Diameter of Binary Tree (LeetCode 543)
Diameter of Binary Tree (LeetCode 543): measure the longest path, counted in edges, between any two nodes of a binary tree. The path does not have to pass through the root.
Examples
Path is [4,2,1,3] or [5,2,1,3], length 3 (3 edges).
Problem Understanding
The diameter of a tree is the number of edges on the longest path between any two nodes.
Crucially, the path does NOT need to pass through the root. It could be entirely contained within the left or right subtree.
Algorithm Strategy
We use Post-Order DFS.
At any node, the longest path that bends at that node has left + right edges, where left and right are the heights of its two subtrees. The longest path overall can bend at any node, not only the root.
So:
- Return Value: the height of the subtree,
max(left, right) + 1: a parent can extend only one side. - Side Effect:
best = max(best, left + right).
Interactive Visualization
h = HEIGHT RETURNED · GREEN = LONGEST PATH SO FAR
best = 0
best
0
Diameter
:
call stack
No calls in progress
best = 0
Next
Call height on the root, 1
Tree
Heights appear under nodes as they return. The longest path found so far is drawn in green, and it moves only when some node's left + right beats it: often at a node that is not the root.
Dry Run: [1,2,3,4,5]
| Node | Left H | Right H | Path Through Me (L+R) | Max Diameter | Return H |
|---|---|---|---|---|---|
| 4 | 0 | 0 | 0 | 0 | 1 |
| 5 | 0 | 0 | 0 | 0 | 1 |
| 2 | 1 (from 4) | 1 (from 5) | 1+1=2 | 2 | 2 |
| 3 | 0 | 0 | 0 | 2 | 1 |
| 1 | 2 (from 2) | 1 (from 3) | 2+1=3 | 3 | 3 |
Edge Cases & Common Mistakes
- Width in Edges vs Nodes: The diameter is usually defined as the number of edges. If asked for nodes, it's
edges + 1. - Diameter not through Root: The longest path might just exist in the left subtree. We must track
maxDiameterglobally (or pass it up). - Single Node: Diameter is 0 edges (1 node).
- Height vs Diameter: The function returns
Height(depth) but updatesDiameteras a side effect. Don't confuse the two.
Solution
Height calculation with Diameter update.
Diameter of Binary Tree Code
1var diameterOfBinaryTree = function(root) {2 let best = 0;34 function height(node) {5 if (!node) return 0;6 const left = height(node.left);7 const right = height(node.right);8 best = Math.max(best, left + right);9 return Math.max(left, right) + 1;10 }1112 height(root);13 return best;14};
Diameter of Binary Tree in Python, Java, C++, TypeScript and C
Diameter of Binary Tree in Python
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
self.best = 0
def height(node):
if not node:
return 0
left = height(node.left)
right = height(node.right)
self.best = max(self.best, left + right)
return max(left, right) + 1
height(root)
return self.bestDiameter of Binary Tree in Java
class Solution {
private int best;
public int diameterOfBinaryTree(TreeNode root) {
best = 0;
height(root);
return best;
}
private int height(TreeNode node) {
if (node == null) return 0;
int left = height(node.left);
int right = height(node.right);
best = Math.max(best, left + right);
return Math.max(left, right) + 1;
}
}Diameter of Binary Tree in C++
class Solution {
int best;
public:
int diameterOfBinaryTree(TreeNode* root) {
best = 0;
height(root);
return best;
}
int height(TreeNode* node) {
if (!node) return 0;
int left = height(node->left);
int right = height(node->right);
best = max(best, left + right);
return max(left, right) + 1;
}
};Diameter of Binary Tree in TypeScript
function diameterOfBinaryTree(root: TreeNode | null): number {
let best = 0;
function height(node: TreeNode | null): number {
if (!node) return 0;
const left = height(node.left);
const right = height(node.right);
best = Math.max(best, left + right);
return Math.max(left, right) + 1;
}
height(root);
return best;
}Diameter of Binary Tree in C
static int best;
static int height(struct TreeNode* node) {
if (node == NULL) return 0;
int left = height(node->left);
int right = height(node->right);
if (left + right > best) best = left + right;
return (left > right ? left : right) + 1;
}
int diameterOfBinaryTree(struct TreeNode* root) {
best = 0;
height(root);
return best;
}More practice: DSA interview questions with answers · patterns cheat sheet
