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, then the diameter is the maximum of: left height + right height (path through node), left subtree diameter, and right subtree diameter.
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²) time complexity because for each node, it calculates height which takes O(n), and this is done for all 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 height and updates diameter at the same time, avoiding repeated height calculations.
Click to reveal answer
beginner
In the context of diameter of binary tree, what does the height of a node represent?
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?
ANumber of nodes in the tree
BHeight of the tree
CNumber of leaf nodes
DLongest 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 always passes through the root
BIt is the path with the most leaf nodes
CIt may or may not pass through the root
DIt is always the leftmost path
✗ Incorrect
The diameter path may or may not pass through the root node.
What is the main cause of O(n²) time complexity in the naive diameter calculation?
AUsing extra memory
BRepeated height calculations for each node
CTraversing only leaf nodes
DIgnoring the root node
✗ Incorrect
Calculating height repeatedly for each node causes O(n²) time.
How does the optimized approach improve the diameter calculation?
ABy calculating diameter and height in one traversal
BBy ignoring leaf nodes
CBy using iterative loops only
DBy calculating diameter after height calculation
✗ Incorrect
The optimized approach calculates height and updates diameter in a single recursive traversal.
What does the height of a node in a binary tree measure?
ANumber of edges from node to deepest leaf
BNumber of nodes in the subtree
CDistance from root to node
DNumber of children of the node
✗ Incorrect
Height is the number of edges on the longest path from the node to a leaf.
Explain how to find the diameter of a binary tree using a recursive approach.
Think about combining height and diameter calculations.
You got /3 concepts.
Describe how to optimize the diameter calculation to avoid repeated height calculations.
Combine height and diameter in one traversal.
You got /4 concepts.
Practice
(1/5)
1. What does the diameter of a binary tree represent?
easy
A. The total number of nodes in the tree
B. The height of the tree
C. The length of the longest path between any two nodes in the tree
D. The number of leaf 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 longest path from root to leaf, leaf count is number of leaves, total nodes is size. Diameter is distinct.
Final Answer:
The length of the longest path between any two nodes in the tree -> Option C
Quick Check:
Diameter = longest path length [OK]
Hint: Diameter is longest path, not height or node count [OK]
Common Mistakes:
Confusing diameter with height
Thinking diameter counts nodes instead of edges
Assuming diameter always passes through root
2. Which of the following C function signatures is correct for a helper function that returns the height of a binary tree node for diameter calculation?
easy
A. int height(struct Node* root);
B. void height(struct Node root);
C. int height(Node root);
D. int height(struct Node root);
Solution
Step 1: Check parameter type for tree node
In C, tree nodes are passed as pointers to struct Node, so parameter should be struct Node* root.
Step 2: Check return type and parameter correctness
Height returns int, so return type int is correct. Passing pointer is correct, not struct by value.
Final Answer:
int height(struct Node* root); -> Option A
Quick Check:
Pointer parameter and int return type [OK]
Hint: Use pointer to struct Node for tree nodes in C [OK]
Common Mistakes:
Passing struct Node by value instead of pointer
Using void return type instead of int
Missing struct keyword in parameter
3. What is the output of the following C code snippet that calculates the diameter of a binary tree?
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int height(struct Node* root) {
if (!root) return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}
int diameter(struct Node* 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);
int maxDiameter = leftHeight + rightHeight + 1;
if (leftDiameter > maxDiameter) maxDiameter = leftDiameter;
if (rightDiameter > maxDiameter) maxDiameter = rightDiameter;
return maxDiameter;
}
int main() {
struct Node n1 = {1, NULL, NULL};
struct Node n2 = {2, NULL, NULL};
struct Node n3 = {3, &n1, &n2};
printf("%d", diameter(&n3));
return 0;
}
medium
A. 1
B. 2
C. 4
D. 3
Solution
Step 1: Analyze tree structure
Root n3 has two children n1 and n2, both leaves. Height of left and right subtree is 1 each.
Step 2: Calculate diameter
Diameter = max of (leftHeight + rightHeight + 1, leftDiameter, rightDiameter) = max(1+1+1, 1, 1) = 3.
4. Identify the bug in this C function that attempts to compute the diameter of a binary tree:
int diameter(struct Node* 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);
int maxDiameter = leftHeight + rightHeight;
if (leftDiameter > maxDiameter) maxDiameter = leftDiameter;
if (rightDiameter > maxDiameter) maxDiameter = rightDiameter;
return maxDiameter;
}
medium
A. height function is not called correctly
B. maxDiameter should be leftHeight + rightHeight + 1
C. Function should return height, not diameter
D. Missing base case for leaf nodes
Solution
Step 1: Check diameter calculation formula
Diameter includes edges between nodes, so it should add 1 for the root node connecting left and right paths.
Step 2: Identify missing +1 in maxDiameter
Current code misses +1, so maxDiameter undercounts by 1.
Final Answer:
maxDiameter should be leftHeight + rightHeight + 1 -> Option B
Quick Check:
Diameter = leftHeight + rightHeight + 1 [OK]
Hint: Add 1 to sum of heights for diameter at root [OK]
Common Mistakes:
Omitting +1 in diameter calculation
Confusing height and diameter returns
Not handling null root base case
5. Given a binary tree where each node stores an integer, how would you modify the diameter calculation to find the diameter based on the sum of node values along the longest path instead of the number of nodes?
hard
A. Change height function to return max sum of node values from root to leaf, and update diameter to sum left and right max sums plus root's value
B. Use the existing diameter function but multiply the result by the root node's value
C. Replace all node values with 1 and then calculate diameter normally
D. Calculate diameter normally and then add all node values in the tree separately
Solution
Step 1: Modify height to return max sum path
Instead of counting nodes, height returns max sum of node values from root to leaf recursively.
Step 2: Update diameter to sum left and right max sums plus root value
Diameter becomes max of leftDiameter, rightDiameter, and leftSum + rightSum + root's value.
Final Answer:
Change height function to return max sum of node values from root to leaf, and update diameter to sum left and right max sums plus root's value -> Option A
Quick Check:
Diameter by sum = max path sum through root [OK]
Hint: Use max sum path instead of node count for diameter sum [OK]
Common Mistakes:
Multiplying diameter by root value incorrectly
Ignoring node values in diameter calculation
Adding all node values separately instead of path sums
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.