The diameter of a binary tree is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root.
Click to reveal answer
intermediate
How do you calculate the diameter of a binary tree using recursion?
Calculate the height of left and right subtrees for each node. The diameter at that node is left height + right height. The maximum diameter found during traversal is the tree's diameter.
Click to reveal answer
beginner
Why might the longest path in a binary tree not pass through the root?
Because the longest path can be between two nodes in the same subtree, not necessarily involving the root node.
Click to reveal answer
intermediate
What is the time complexity of the standard recursive approach to find the diameter of a binary tree?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the traversal.
Click to reveal answer
beginner
In the diameter calculation, what does the height of a node represent?
The height of a node is the number of edges on the longest path from that node down to a leaf node.
Click to reveal answer
What does the diameter of a binary tree represent?
AThe number of leaf nodes
BThe height of the tree
CThe number of nodes in the tree
DThe longest path between any two nodes
✗ Incorrect
The diameter is the longest path between any two nodes in the tree.
Which of the following is true about the diameter path in a binary tree?
AIt never passes through the root
BIt may or may not pass through the root
CIt always passes through the root
DIt is always the path from root to the deepest leaf
✗ Incorrect
The diameter path may or may not pass through the root node.
What is the key information needed at each node to compute the diameter?
AHeight of left and right subtrees
BNumber of children nodes
CValue of the node
DDepth of the node
✗ Incorrect
Height of left and right subtrees is used to calculate the diameter at each node.
What is the time complexity of the efficient diameter calculation algorithm?
AO(n^2)
BO(n log n)
CO(n)
DO(log n)
✗ Incorrect
The efficient algorithm visits each node once, so time complexity is O(n).
In the diameter calculation, what does the height of a node count?
ANumber of edges from node to leaf
BNumber of nodes in the subtree
CNumber of children
DDepth from root
✗ Incorrect
Height counts the number of edges on the longest path from the node down to a leaf.
Explain how to find the diameter of a binary tree using recursion.
Think about how height and diameter relate at each node.
You got /5 concepts.
Why might the longest path in a binary tree not include the root node?
Consider the shape of the tree and where longest paths can be.
You got /3 concepts.
Practice
(1/5)
1. What does the diameter of a binary tree represent?
easy
A. The length of the longest path between any two nodes in the tree
B. The number of nodes in the tree
C. The height of the tree
D. The number of leaf nodes
Solution
Step 1: Understand the diameter definition
The diameter is the longest path between any two nodes, not just from root to leaf.
Step 2: Differentiate from height and node count
Height is longest root-to-leaf path; node count is total nodes; diameter can be longer than height.
Final Answer:
The length of the longest path between any two nodes in the tree -> Option A
Quick Check:
Diameter = longest path length [OK]
Hint: Diameter is longest path between any two nodes, not just height [OK]
Common Mistakes:
Confusing diameter with height
Thinking diameter counts nodes instead of edges
Assuming diameter is always root to leaf
2. Which of the following Python function signatures is correct for computing the diameter of a binary tree node?
easy
A. def diameter(root: int) -> TreeNode:
B. def diameter(root: Optional[TreeNode]) -> int:
C. def diameter(root: TreeNode) -> str:
D. def diameter(root: List[int]) -> int:
Solution
Step 1: Identify correct parameter and return types
Function takes a tree node or None and returns an integer diameter length.
Step 2: Check each option's signature
def diameter(root: Optional[TreeNode]) -> int: matches expected input (node or None) and output (int). Others mismatch types.
Final Answer:
def diameter(root: Optional[TreeNode]) -> int: -> Option B
Quick Check:
Input node, output int diameter [OK]
Hint: Diameter function inputs root node, returns int length [OK]
Common Mistakes:
Using wrong input type like int or list
Returning wrong type like str or TreeNode
Missing Optional for nullable root
3. What is the output of the following code snippet?
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def diameter(root):
diameter_val = 0
def depth(node):
nonlocal diameter_val
if not node:
return 0
left = depth(node.left)
right = depth(node.right)
diameter_val = max(diameter_val, left + right)
return max(left, right) + 1
depth(root)
return diameter_val
root = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3))
print(diameter(root))
medium
A. 2
B. 4
C. 3
D. 5
Solution
Step 1: Understand tree structure and depth calls
Tree has root 1, left subtree with nodes 2,4,5 and right subtree with node 3. Longest path is 4->2->1->3 or 5->2->1->3 with length 3 edges.
Step 2: Calculate diameter_val during recursion
At node 2, left and right depths are 1 each, sum 2; at root, left depth 2, right depth 1, sum 3 updates diameter_val to 3.
Final Answer:
3 -> Option C
Quick Check:
Longest path edges = 3 [OK]
Hint: Diameter counts edges on longest path, not nodes [OK]
Common Mistakes:
Counting nodes instead of edges for diameter
Ignoring nonlocal update of diameter_val
Misunderstanding tree structure
4. Find the error in the following diameter function and select the fix:
def diameter(root):
diameter_val = 0
def depth(node):
if not node:
return 0
left = depth(node.left)
right = depth(node.right)
diameter_val = max(diameter_val, left + right)
return max(left, right) + 1
depth(root)
return diameter_val
medium
A. Initialize diameter_val inside depth() function
B. Change 'diameter_val' to a global variable
C. Return diameter_val inside depth() function
D. Add 'nonlocal diameter_val' inside depth() to update outer variable
Solution
Step 1: Identify variable scope issue
diameter_val is assigned inside depth() but not declared nonlocal, so assignment creates local variable, not updating outer diameter_val.
5. Given a binary tree where some nodes have only one child, how does the diameter calculation handle paths through these nodes?
Choose the correct statement about the diameter in such cases.
hard
A. Diameter counts the longest path even if it passes through nodes with only one child
B. Diameter ignores nodes with only one child when calculating longest path
C. Diameter is always the height of the tree when nodes have one child
D. Diameter equals the number of leaf nodes in the tree
Solution
Step 1: Understand diameter path definition
Diameter is longest path between any two nodes, regardless of node children count.
Step 2: Consider nodes with one child
Paths can pass through nodes with one child; these nodes do not exclude paths from consideration.
Final Answer:
Diameter counts the longest path even if it passes through nodes with only one child -> Option A
Quick Check:
Diameter includes all nodes on longest path [OK]
Hint: Diameter includes all nodes on longest path, child count doesn't exclude [OK]
Common Mistakes:
Assuming nodes with one child are ignored
Confusing diameter with height in skewed trees
Thinking diameter equals leaf count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.