Problem
You get the root root of a binary tree. In a binary tree, each node has at most two children: a left child and a right child.
Return the diameter of the tree. The diameter is the length of the longest path between any two nodes. The length of a path is the number of edges on it. An edge is the link between a node and its child.
The longest path can go through the root, but it does not have to.
The examples show each tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child.
Example 1
- Input:
- root = [6,3,9,2,5,null,12]
- Output:
- 4
- Explanation:
- One longest path is
2 -> 3 -> 6 -> 9 -> 12. It has 4 edges.
Example 2
- Input:
- root = [7,4,null,3,5,1,null,null,8]
- Output:
- 4
- Explanation:
- The path
1 -> 3 -> 4 -> 5 -> 8has 4 edges. It does not go through the root7.
Example 3
- Input:
- root = [9]
- Output:
- 0
- Explanation:
- A tree with one node has no edges.
Constraints
- 1 <= number of nodes <= 10^4
- -100 <= Node.val <= 100
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Binary Tree Cameras
Coding · Binary Tree · Depth-First Search · DP on Trees
- Binary Tree Maximum Path Sum
Coding · Binary Tree · Depth-First Search · DP on Trees
- All Nodes Distance K in Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
- Amount of Time for Binary Tree to Be Infected
Coding · Binary Tree · Breadth-First Search · Depth-First Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Meta
Leetcode 543. Diameter of Binary Tree
Reported Apr 6, 2026 · 26 source reports
JUNIOR, MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceOracle
Leetcode 543. Diameter of Binary Tree
Reported Sep 17, 2026 · 1 source report
PRINCIPAL
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 3 records
Meta
Diameter of Binary Tree
Reported date not specified · Source window: 30d
Source snapshot: Oct 20, 2026
Source frequency score: 72.1. This is not an interview probability.
View sourceRoblox
Diameter of Binary Tree