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 overall diameter is the maximum of these values across all nodes.
Click to reveal answer
intermediate
What is the time complexity of the naive approach to find the diameter of a binary tree?
The naive approach has O(n^2) time complexity because for each node, it calculates height which takes O(n), and there are n nodes.
Click to reveal answer
advanced
How can you optimize the diameter calculation to O(n) time?
Use a single recursive function that returns both height and updates the diameter during traversal, so each node is visited once.
Click to reveal answer
beginner
In the diameter calculation, why might the longest path not pass through the root?
Because the longest path can be between two nodes in the left or right subtree, not necessarily passing through the root 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 longest path between any two nodes
DThe number of nodes in the tree
✗ 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 always passes through the root
BIt is always the path from root to a leaf
CIt never passes through the root
DIt may or may not pass through the root
✗ Incorrect
The diameter path may or may not pass through the root node.
What is the main cause of O(n^2) time complexity in the naive diameter calculation?
ARepeated calculation of height for each node
BUsing extra space for recursion
CIterating over leaf nodes only
DSorting nodes before calculation
✗ Incorrect
Calculating height repeatedly for each node causes O(n^2) time complexity.
How can you reduce the time complexity of diameter calculation to O(n)?
ACalculate height and diameter in one traversal
BUse iterative traversal only
CCalculate diameter only at leaf nodes
DUse breadth-first search
✗ Incorrect
Calculating height and updating diameter in one traversal reduces time complexity to O(n).
In diameter calculation, what does the sum of left height and right height at a node represent?
ANumber of nodes in the tree
BLength of longest path through that node
CHeight of the tree
DDepth of the node
✗ Incorrect
Sum of left and right heights at a node gives the length of the longest path passing through that node.
Explain how to find the diameter of a binary tree using a single recursive function.
Think about combining height calculation and diameter update in one step.
You got /4 concepts.
Why might the longest path in a binary tree not pass through the root node?
Consider where the longest path can be located in the tree.
You got /4 concepts.
Practice
(1/5)
1. What does the diameter of a binary tree represent?
easy
A. The longest path between any two nodes in the tree
B. The height of the tree
C. The number of leaf nodes
D. The total number of nodes in the tree
Solution
Step 1: Understand the diameter concept
The diameter is the longest path between any two nodes, which may or may not pass through the root.
Step 2: Differentiate from height and node count
Height is the longest path from root to leaf, and node count is total nodes, both different from diameter.
Final Answer:
The longest path between any two nodes in the tree -> Option A
Quick Check:
Diameter = Longest path between nodes [OK]
Hint: Diameter is longest path, not just height or node count [OK]
Common Mistakes:
Confusing diameter with tree height
Thinking diameter counts nodes instead of edges
Assuming diameter must pass through root
2. Which of the following JavaScript function signatures is correct for computing the diameter of a binary tree node?
easy
A. function diameterTree() {}
B. function diameter(root, tree) {}
C. function binaryTreeDiameter(node, root) {}
D. function diameterOfBinaryTree(root) {}
Solution
Step 1: Check common parameter naming
The function usually takes the root node as input, so a single parameter named root is standard.
Step 2: Validate function signature correctness
function diameterOfBinaryTree(root) {} uses a clear, single parameter 'root' and a descriptive function name, matching common practice.
Final Answer:
function diameterOfBinaryTree(root) {} -> Option D
Quick Check:
Correct function signature uses root parameter [OK]
Hint: Function usually takes root node as single parameter [OK]
Common Mistakes:
Using multiple unrelated parameters
Incorrect or unclear function names
Missing parameters for the tree root
3. Given the following binary tree structure and code, what is the diameter of the tree?
class TreeNode {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
const root = new TreeNode(1,
new TreeNode(2,
new TreeNode(4),
new TreeNode(5)
),
new TreeNode(3)
);
function diameterOfBinaryTree(root) {
let diameter = 0;
function depth(node) {
if (!node) return 0;
const left = depth(node.left);
const right = depth(node.right);
diameter = Math.max(diameter, left + right);
return Math.max(left, right) + 1;
}
depth(root);
return diameter;
}
console.log(diameterOfBinaryTree(root));
medium
A. 4
B. 3
C. 2
D. 5
Solution
Step 1: Analyze the tree structure
The tree has root 1, left subtree with nodes 2,4,5 and right subtree with node 3. The longest path is from node 4 to node 3 through 2 and 1 (4->2->1->3), which has length 3 edges.
Step 2: Trace the code logic
The code calculates diameter as max sum of left and right depths at each node. At node 2, left and right depths are 1 each, so diameter updates to 2. At root, left depth is 2, right depth is 1, so diameter updates to 3.
Final Answer:
3 -> Option B
Quick Check:
Longest path edges = 3 [OK]
Hint: Diameter = max sum of left and right subtree depths [OK]
Common Mistakes:
Counting nodes instead of edges for diameter
Ignoring paths not passing through root
Misreading tree structure
4. The following code attempts to compute the diameter of a binary tree but returns incorrect results. What is the bug?
function diameterOfBinaryTree(root) {
let diameter = 0;
function depth(node) {
if (!node) return 0;
const left = depth(node.left);
const right = depth(node.right);
diameter = Math.max(diameter, left * right);
return Math.max(left, right) + 1;
}
depth(root);
return diameter;
}
medium
A. Using multiplication instead of addition for diameter update
B. Returning max depth instead of sum of depths
C. Not handling null nodes correctly
D. Missing base case for leaf nodes
Solution
Step 1: Identify diameter update logic
The code updates diameter using left * right, which multiplies depths instead of adding them.
Step 2: Correct diameter calculation
Diameter should be updated with left + right, representing the longest path through the node.
Final Answer:
Using multiplication instead of addition for diameter update -> Option A
Quick Check:
Diameter update uses addition, not multiplication [OK]
Hint: Diameter update = left depth + right depth, not multiplication [OK]
Common Mistakes:
Multiplying depths instead of adding
Confusing depth return with diameter update
Ignoring base case for null nodes
5. You want to find the diameter of a binary tree but also return the path nodes that form this diameter. Which approach correctly extends the diameter calculation to return the path?
hard
A. Use breadth-first search to find the longest path from root to any leaf
B. Only compute the height of the tree and return the nodes at the deepest level
C. Track left and right subtree depths and their paths; update diameter and store combined path when a longer diameter is found
D. Count total nodes and return all leaf nodes as the diameter path
Solution
Step 1: Understand diameter path requirements
Diameter path is the longest path between any two nodes, so we must track paths from left and right subtrees.
Step 2: Extend depth function to track paths
Along with depths, store paths for left and right subtrees. When diameter updates, combine these paths to form the diameter path.
Step 3: Reject incorrect approaches
Height alone or BFS from root won't find longest path between any two nodes. Counting leaf nodes is unrelated.
Final Answer:
Track left and right subtree depths and their paths; update diameter and store combined path when a longer diameter is found -> Option C
Quick Check:
Track depths and paths to get diameter path [OK]
Hint: Store paths with depths; update when diameter increases [OK]
Common Mistakes:
Only tracking height or BFS from root
Ignoring path storage during recursion
Assuming diameter path is always root to leaf
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.