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 the sum of these heights plus two (for the edges connecting the node to its subtrees). The overall diameter is the maximum of these values across all nodes.
Click to reveal answer
intermediate
Why do we use a helper function to calculate height when finding the diameter?
Because the diameter depends on the heights of subtrees, a helper function calculates height efficiently while updating the maximum diameter found so far during traversal.
Click to reveal answer
intermediate
What is the time complexity of the diameter of binary tree algorithm using a single traversal?
The time complexity is O(n), where n is the number of nodes, because each node is visited once to calculate height and update diameter.
Click to reveal answer
beginner
Can the diameter path pass through the root node?
Yes, the diameter path can pass through the root node, but it is not necessary. The longest path can be anywhere in the tree.
Click to reveal answer
What does the diameter of a binary tree represent?
AThe longest path between any two nodes
BThe height of the tree
CThe number of leaf 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 never passes through the root
CIt may or may not pass through the root
DIt is always the path from root to a leaf
✗ Incorrect
The diameter path may or may not pass through the root node.
What is the main idea to find the diameter of a binary tree efficiently?
ACalculate height and diameter in one traversal
BCalculate diameter by counting leaf nodes
CUse breadth-first search only
DCalculate diameter by summing node values
✗ Incorrect
Calculating height and diameter in one traversal avoids repeated work and improves efficiency.
What is the time complexity of the optimal diameter of binary tree algorithm?
AO(n^2)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
The optimal algorithm visits each node once, so it runs in O(n) time.
In the diameter calculation, what does the sum of left and right subtree heights represent?
AThe number of leaf nodes
BThe number of nodes in the tree
CThe height of the tree
DThe length of the longest path through the current node
✗ Incorrect
The sum of left and right subtree heights plus one represents the longest path through the current node.
Explain how to find the diameter of a binary tree using recursion and height calculation.
Think about how height helps find longest paths.
You got /5 concepts.
Describe why the diameter path may not pass through the root node in a binary tree.
Consider different shapes of trees.
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 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 and leaf count are unrelated to diameter.
Final Answer:
The longest path between any two nodes in the tree -> Option A
Quick Check:
Diameter = longest node-to-node path [OK]
Hint: Diameter is longest path between any two nodes [OK]
Common Mistakes:
Confusing diameter with tree height
Thinking diameter counts nodes instead of edges
Assuming diameter is number of leaf nodes
2. Which of the following is the correct C++ function signature to compute the diameter of a binary tree node?
easy
A. int diameter(TreeNode& root);
B. void diameter(TreeNode root);
C. int diameter(TreeNode root);
D. int diameter(TreeNode* root);
Solution
Step 1: Understand pointer usage in tree nodes
Binary trees are usually handled with pointers to nodes, so TreeNode* is correct.
Step 2: Check return type and parameter correctness
Diameter returns an int, and parameter should be pointer, not value or reference for typical tree traversal.
Final Answer:
int diameter(TreeNode* root); -> Option D
Quick Check:
Pointer parameter with int return [OK]
Hint: Use pointer parameter and int return type [OK]
Common Mistakes:
Passing node by value instead of pointer
Using void return type instead of int
Passing node by reference unnecessarily
3. What is the output of the following code snippet computing diameter?
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int height(TreeNode* root) {
if (!root) return 0;
return 1 + std::max(height(root->left), height(root->right));
}
int diameter(TreeNode* root) {
if (!root) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
int leftDiameter = diameter(root->left);
int rightDiameter = diameter(root->right);
return std::max(leftHeight + rightHeight, std::max(leftDiameter, rightDiameter));
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
std::cout << diameter(root);
return 0;
}
medium
A. 6
B. 4
C. 3
D. 5
Solution
Step 1: Calculate heights of left and right subtrees
Left subtree height: max height of nodes 2->4 or 2->5 is 2; right subtree height (node 3) is 1.
Step 2: Calculate diameter candidates and choose max
Diameter candidates: leftHeight + rightHeight = 2 + 1 = 3; leftDiameter and rightDiameter are computed recursively. LeftDiameter is 2 (path 4-2-5), rightDiameter is 0. Max is 3.
Final Answer:
3 -> Option C
Quick Check:
Longest path length = 3 edges [OK]
Hint: Sum left and right subtree heights for diameter [OK]
Common Mistakes:
Confusing height with diameter
Counting nodes instead of edges
Ignoring recursive diameter calls
4. Identify the bug in this diameter function and select the fix:
int diameter(TreeNode* root) {
if (!root) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
int leftDiameter = diameter(root->left);
int rightDiameter = diameter(root->right);
return leftHeight + rightHeight;
}
medium
A. Return max of leftDiameter and rightDiameter only
B. Return max of leftHeight + rightHeight and leftDiameter, rightDiameter
C. Return sum of leftDiameter and rightDiameter
D. Return height of root node
Solution
Step 1: Analyze current return statement
Currently returns only leftHeight + rightHeight, ignoring recursive diameters from subtrees.
Step 2: Correct return to consider all diameter candidates
Diameter is max of leftHeight + rightHeight, leftDiameter, and rightDiameter.
Final Answer:
Return max of leftHeight + rightHeight and leftDiameter, rightDiameter -> Option B
Quick Check:
Include all diameter candidates [OK]
Hint: Return max of height sum and subtree diameters [OK]
Common Mistakes:
Ignoring recursive diameter calls
Returning sum of diameters incorrectly
Returning height instead of diameter
5. Given a binary tree where each node has a value, how would you modify the diameter function to return the maximum sum of node values on any path between two nodes instead of the number of edges?
hard
A. Change height function to return max sum of node values and update diameter to use sums instead of heights
B. Use the same diameter function but multiply the result by node values
C. Calculate diameter normally and add all node values in the tree
D. Return the maximum node value in the tree as diameter
Solution
Step 1: Modify height to return max sum of node values
Instead of counting edges, height returns max sum from node to leaf including node values.
Step 2: Update diameter to use sums from left and right subtrees
Diameter becomes max of left sum + right sum and recursive diameters, reflecting max path sum.
Final Answer:
Change height function to return max sum of node values and update diameter to use sums instead of heights -> Option A
Quick Check:
Use sums in height and diameter for max path value [OK]
Hint: Use sums in height and diameter for max path value [OK]
Common Mistakes:
Multiplying diameter by node values incorrectly
Adding all node values regardless of path
Returning max node value instead of path sum
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.