Challenge - 5 Problems
Tree Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of diameter calculation on a simple tree
What is the output of the following C++ code that calculates the diameter of a tree using DP on trees?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<vector<int>> tree; int diameter = 0; int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (int child : tree[node]) { if (child == parent) continue; int depth = dfs(child, node) + 1; if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = max(diameter, max1 + max2); return max1; } int main() { int n = 5; tree.resize(n); tree[0].push_back(1); tree[1].push_back(0); tree[1].push_back(2); tree[2].push_back(1); tree[1].push_back(3); tree[3].push_back(1); tree[3].push_back(4); tree[4].push_back(3); dfs(0, -1); cout << diameter << endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the longest path between any two nodes in the tree.
✗ Incorrect
The longest path (diameter) in the tree is from node 2 to node 4 passing through nodes 1 and 3, which has length 4 edges.
🧠 Conceptual
intermediateUnderstanding diameter calculation logic
In the DP on trees approach to find the diameter, what do the two maximum depths (max1 and max2) represent in the DFS function?
Attempts:
2 left
💡 Hint
Think about how the diameter is formed by combining two longest paths through a node.
✗ Incorrect
max1 and max2 store the lengths of the two longest downward paths from the current node to its descendants, which help compute the diameter passing through this node.
🔧 Debug
advancedIdentify the error causing wrong diameter output
The following code attempts to compute the diameter of a tree but outputs 3 instead of 4. What is the error?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<vector<int>> tree; int diameter = 0; int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (int child : tree[node]) { if (child == parent) continue; int depth = dfs(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = max(diameter, max1 + max2); return max1 + 1; } int main() { int n = 5; tree.resize(n); tree[0].push_back(1); tree[1].push_back(0); tree[1].push_back(2); tree[2].push_back(1); tree[1].push_back(3); tree[3].push_back(1); tree[3].push_back(4); tree[4].push_back(3); dfs(0, -1); cout << diameter << endl; return 0; }
Attempts:
2 left
💡 Hint
Check where the +1 is added to the depth returned by recursive calls.
✗ Incorrect
The code returns depth without adding 1 after recursion, so max1 and max2 are off by one, causing diameter to be smaller by 1.
📝 Syntax
advancedIdentify the syntax error in diameter calculation code
Which option contains a syntax error that prevents the code from compiling?
DSA C++
int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (int child : tree[node]) { if (child == parent) continue; int depth = dfs(child, node) + 1 if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = max(diameter, max1 + max2); return max1; }
Attempts:
2 left
💡 Hint
Look carefully at the line where depth is assigned.
✗ Incorrect
The line 'int depth = dfs(child, node) + 1' is missing a semicolon at the end, causing a syntax error.
🚀 Application
expertCalculate diameter of a weighted tree using DP on trees
Given the weighted tree below, what is the diameter (maximum sum of edge weights on any path)?
Edges:
0 - 1 (weight 3)
1 - 2 (weight 4)
1 - 3 (weight 2)
3 - 4 (weight 6)
Use the DP on trees approach where dfs returns the maximum weighted path from node to descendants.
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Edge { int to, weight; }; vector<vector<Edge>> tree; int diameter = 0; int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (auto &edge : tree[node]) { if (edge.to == parent) continue; int depth = dfs(edge.to, node) + edge.weight; if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } diameter = max(diameter, max1 + max2); return max1; } int main() { int n = 5; tree.resize(n); tree[0].push_back({1, 3}); tree[1].push_back({0, 3}); tree[1].push_back({2, 4}); tree[2].push_back({1, 4}); tree[1].push_back({3, 2}); tree[3].push_back({1, 2}); tree[3].push_back({4, 6}); tree[4].push_back({3, 6}); dfs(0, -1); cout << diameter << endl; return 0; }
Attempts:
2 left
💡 Hint
Find the longest weighted path between any two nodes by combining two longest weighted paths from a node.
✗ Incorrect
The longest weighted path is 2 -> 1 -> 3 -> 4 with weights 4 + 2 + 6 = 12. The diameter is 13 because at node 1, max1 is 8 (path to 4 via 3) and max2 is 5 (path to 2), sum is 13.
