Challenge - 5 Problems
Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Diameter Calculation for Simple Tree
What is the output of the following C code that calculates the diameter of a binary tree?
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
int max(int a, int b) {
return (a > b) ? a : b;
}
int height(Node* root) {
if (!root) return 0;
return 1 + max(height(root->left), height(root->right));
}
int diameter(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 = max(leftDiameter, rightDiameter);
return max(leftHeight + rightHeight + 1, maxDiameter);
}
int main() {
Node n4 = {4, NULL, NULL};
Node n5 = {5, NULL, NULL};
Node n2 = {2, &n4, &n5};
Node n3 = {3, NULL, NULL};
Node n1 = {1, &n2, &n3};
int d = diameter(&n1);
printf("%d\n", d);
return 0;
}Attempts:
2 left
💡 Hint
Think about the longest path between two nodes in the tree.
✗ Incorrect
The diameter is the longest path between any two nodes. Here, the longest path goes through nodes 4 -> 2 -> 1 -> 3, which has length 4.
❓ Predict Output
intermediateDiameter Output for Unbalanced Tree
What is the output of the diameter function for this unbalanced binary tree?
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
int max(int a, int b) {
return (a > b) ? a : b;
}
int height(Node* root) {
if (!root) return 0;
return 1 + max(height(root->left), height(root->right));
}
int diameter(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 = max(leftDiameter, rightDiameter);
return max(leftHeight + rightHeight + 1, maxDiameter);
}
int main() {
Node n5 = {5, NULL, NULL};
Node n4 = {4, &n5, NULL};
Node n3 = {3, &n4, NULL};
Node n2 = {2, &n3, NULL};
Node n1 = {1, &n2, NULL};
int d = diameter(&n1);
printf("%d\n", d);
return 0;
}Attempts:
2 left
💡 Hint
The tree is a straight line, so diameter equals the number of nodes.
✗ Incorrect
The tree is a chain of 5 nodes, so the longest path is from leaf to leaf through all nodes, length 5.
🔧 Debug
advancedIdentify the Bug in Diameter Calculation
The following code aims to calculate the diameter of a binary tree but produces incorrect results. What is the main bug?
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
int max(int a, int b) {
return (a > b) ? a : b;
}
int height(Node* root) {
if (!root) return 0;
return 1 + max(height(root->left), height(root->right));
}
int diameter(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 = max(leftDiameter, rightDiameter);
return max(leftHeight + rightHeight, maxDiameter);
}
Attempts:
2 left
💡 Hint
Check how the diameter is computed from heights of left and right subtrees.
✗ Incorrect
The diameter should count the current node as part of the path, so it must add 1 to leftHeight + rightHeight. Missing this causes undercounting.
🚀 Application
advancedCalculate Diameter for Given Tree Structure
Given the binary tree below, what is the diameter?
Tree structure:
- Root node 1
- Left child 2 with left child 4 and right child 5
- Right child 3 with right child 6
- Node 6 has left child 7
Use the diameter definition as the longest path between any two nodes.
Attempts:
2 left
💡 Hint
Consider the longest path that may pass through the root or deeper nodes.
✗ Incorrect
The longest path is from node 7 to node 5: 7 -> 6 -> 3 -> 1 -> 2 -> 5, length 6.
🧠 Conceptual
expertTime Complexity of Diameter Calculation
What is the time complexity of the naive diameter calculation algorithm that uses separate height calculations for each node?
Attempts:
2 left
💡 Hint
Consider how many times height is called for each node.
✗ Incorrect
Height is called for each node and itself traverses subtrees, causing repeated work leading to O(n^2) complexity.
