543. Diameter of Binary Tree¶
Edited by Charles
Easy · Python · Binary Tree
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given the root of a binary tree, return the diameter of the tree.
The diameter is the length of the longest path between any two nodes in the tree.
Original study notes follow, including intermediate snippets and earlier attempts.
Problem¶
Given the root of a binary tree, return the diameter of the tree.
The diameter is the length of the longest path between any two nodes in the tree.
The path does not necessarily need to pass through the root.
The length of a path is measured by the number of edges, not the number of nodes.
Example¶
The longest path can be:
or:
There are 3 edges.
Therefore:
The key idea is to distinguish between two concepts:
- The diameter passing through the current node
- The maximum depth returned to the parent
This is very similar to LeetCode 124 — Binary Tree Maximum Path Sum.
For every node:
we first recursively calculate:
Then we use these two depths to:
Define:
as:
The maximum depth starting from
nodeand going downward along one branch.
For example:
Starting from 2, the longest downward path is:
Therefore:
Notice that this value represents the number of nodes in the downward path starting from 2.
This is why we return:
The 1 represents the current node.
If the current node does not exist:
An empty subtree has depth:
For a leaf node:
we get:
Therefore:
which gives:
So a leaf node has depth 1.
We recursively calculate:
Because the current node needs information from both children before it can calculate its own result, this is a Postorder DFS:
Suppose:
The longest path passing through node can use:
Therefore, the diameter passing through the current node is:
Why don't we add 1?
Because the diameter is measured by the number of edges.
For example:
At node 1:
The path is:
The number of edges is:
Total:
And:
Therefore:
The longest path does not necessarily pass through the root.
For example:
The longest path may pass through node 2 rather than using the root as its highest point.
Therefore, at every node, we calculate:
and compare it with the current global maximum:
So self.res represents:
The maximum diameter found anywhere in the tree so far.
This is the most important part of the problem.
When calculating the diameter at the current node, we can use both sides:
So:
can be used to update the global answer.
However, when returning a depth to the parent, we cannot return both sides.
Suppose:
If node returned both sides to parent, the structure would become:
This is a Y-shaped structure.
It is not a single path because it branches.
Therefore, when returning to the parent, we must choose only one side:
or:
We choose the deeper one:
This is the core idea of LeetCode 543.
Update the Diameter¶
At the current node, both sides can be used:
Therefore:
Return to Parent¶
When returning upward, only one side can be used:
or:
Therefore:
Consider:
Initially:
Node 4¶
Node 4 is a leaf.
Update diameter:
So:
Return depth:
Node 5¶
Same:
Update:
Return:
Node 2¶
Now:
The complete path passing through 2 is:
Diameter:
Update:
Return to node 1:
This represents the depth:
or:
Node 3¶
Node 3 is a leaf.
Return:
Node 1¶
Now:
The complete path can be:
Diameter:
Update:
Finally:
For every node:
node
/ \
left right
↓
Calculate:
leftDepth = traverse(left)
rightDepth = traverse(right)
↓
Complete path through current node:
leftDepth + rightDepth
↓
Update global diameter:
res = max(res, leftDepth + rightDepth)
↓
Return one branch to parent:
1 + max(leftDepth, rightDepth)
LeetCode 543 and LeetCode 124 have almost exactly the same recursive structure.
LeetCode 124 — Maximum Path Sum¶
Update global answer:
Return to parent:
LeetCode 543 — Diameter¶
Update global answer:
Return to parent:
So both problems follow the same pattern:
Time Complexity¶
Each node is visited exactly once:
where n is the number of nodes.
Space Complexity¶
The recursion stack depends on the height of the tree:
For a balanced tree:
For a completely skewed tree:
The most important distinction is:
because the complete path can go:
But:
because a path returned to the parent cannot branch.
The easiest way to remember LeetCode 543 is:
or simply:
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 14, 2026 |
Python solution¶
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
self.res = 0
self.traverse(root)
return self.res
def traverse(self, node):
if not node:
return 0
leftDepth = self.traverse(node.left)
rightDepth = self.traverse(node.right)
self.res = max(
self.res,
leftDepth + rightDepth
)
return 1 + max(leftDepth, rightDepth)