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 represented as an adjacency list?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int>[] tree; static int maxDiameter = 0; static int DFS(int node, int parent) { int max1 = 0, max2 = 0; foreach (var child in tree[node]) { if (child == parent) continue; int depth = DFS(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } maxDiameter = Math.Max(maxDiameter, max1 + max2); return max1 + 1; } static void Main() { int n = 5; tree = new List<int>[n]; for (int i = 0; i < n; i++) tree[i] = new List<int>(); tree[0].Add(1); tree[1].Add(0); tree[1].Add(2); tree[2].Add(1); tree[1].Add(3); tree[3].Add(1); tree[3].Add(4); tree[4].Add(3); DFS(0, -1); Console.WriteLine(maxDiameter); } }
Attempts:
2 left
💡 Hint
Trace the longest path between any two nodes in the tree.
✗ Incorrect
The longest path (diameter) in this tree is from node 2 to node 4 passing through nodes 1 and 3, which has length 3 edges.
🧠 Conceptual
intermediateUnderstanding Diameter Calculation Logic
In the diameter calculation algorithm using DFS on a tree, why do we keep track of the two longest depths from children of a node?
Attempts:
2 left
💡 Hint
Think about the longest path that goes through the current node.
✗ Incorrect
The diameter can be the longest path passing through the current node, which is the sum of the two longest depths from its children.
❓ Predict Output
advancedOutput of Diameter Calculation on a Star-Shaped Tree
What is the output of the following C# code that calculates the diameter of a star-shaped tree with 4 leaves connected to a center node?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int>[] tree; static int maxDiameter = 0; static int DFS(int node, int parent) { int max1 = 0, max2 = 0; foreach (var child in tree[node]) { if (child == parent) continue; int depth = DFS(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } maxDiameter = Math.Max(maxDiameter, max1 + max2); return max1 + 1; } static void Main() { int n = 5; tree = new List<int>[n]; for (int i = 0; i < n; i++) tree[i] = new List<int>(); // star shape: node 0 connected to 1,2,3,4 for (int i = 1; i < n; i++) { tree[0].Add(i); tree[i].Add(0); } DFS(0, -1); Console.WriteLine(maxDiameter); } }
Attempts:
2 left
💡 Hint
The longest path in a star is between any two leaves through the center.
✗ Incorrect
The diameter is 2 edges: from one leaf to center to another leaf.
🔧 Debug
advancedIdentify the Error in Diameter Calculation Code
What error will occur when running the following C# code to calculate the diameter of a tree?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int>[] tree; static int maxDiameter = 0; static int DFS(int node, int parent) { int max1 = 0, max2 = 0; foreach (var child in tree[node]) { if (child == parent) continue; int depth = DFS(child, node); if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) { max2 = depth; } } maxDiameter = Math.Max(maxDiameter, max1 + max2); return max1 + 1; } static void Main() { int n = 3; tree = new List<int>[n]; for (int i = 0; i < n; i++) tree[i] = new List<int>(); tree[0].Add(1); tree[1].Add(0); tree[1].Add(2); tree[2].Add(1); DFS(3, -1); Console.WriteLine(maxDiameter); } }
Attempts:
2 left
💡 Hint
Check the node index passed to DFS in Main.
✗ Incorrect
DFS is called with node 3 but tree size is 3 (indices 0 to 2), causing IndexOutOfRangeException.
🚀 Application
expertCalculate Diameter for a Complex Tree
Given the following tree edges, what is the diameter of the tree?
Edges:
0-1, 1-2, 1-3, 3-4, 4-5, 5-6
Use the diameter definition as the longest path between any two nodes.
Attempts:
2 left
💡 Hint
Find the longest path by tracing from leaf to leaf through the tree.
✗ Incorrect
The longest path is from node 2 to node 6 passing through nodes 1,3,4,5 with length 6 edges.
