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 Java code that calculates the diameter of a tree represented as an adjacency list?
DSA Java
import java.util.*; public class TreeDiameter { static List<List<Integer>> tree = new ArrayList<>(); static int diameter = 0; public static int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (int child : tree.get(node)) { if (child == parent) continue; int height = dfs(child, node); if (height > max1) { max2 = max1; max1 = height; } else if (height > max2) { max2 = height; } } diameter = Math.max(diameter, max1 + max2); return max1 + 1; } public static void main(String[] args) { int n = 5; for (int i = 0; i < n; i++) tree.add(new ArrayList<>()); tree.get(0).add(1); tree.get(1).add(0); tree.get(1).add(2); tree.get(2).add(1); tree.get(1).add(3); tree.get(3).add(1); tree.get(3).add(4); tree.get(4).add(3); dfs(0, -1); System.out.println(diameter); } }
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
✗ Incorrect
The diameter is the longest path between two nodes. Here, the longest path 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 using DFS on a tree, why do we keep track of the two longest heights from the children of a node?
Attempts:
2 left
💡 Hint
Think about the longest path that can pass through a node connecting two subtrees.
✗ Incorrect
The diameter can be the longest path passing through a node, which is the sum of the two longest heights from its children. Tracking two longest heights helps find this path.
🔧 Debug
advancedIdentify the Error in Diameter Calculation Code
What error will the following code produce when calculating the diameter of a tree?
DSA Java
import java.util.*; public class TreeDiameter { static List<List<Integer>> tree = new ArrayList<>(); static int diameter = 0; public static int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (int child : tree.get(node)) { if (child == parent) continue; int height = dfs(child, node); if (height > max1) { max2 = max1; max1 = height; } else if (height > max2) { max2 = height; } } diameter = Math.max(diameter, max1 + max2); return max1 + 1; } public static void main(String[] args) { int n = 3; for (int i = 0; i < n; i++) tree.add(new ArrayList<>()); tree.get(0).add(1); tree.get(1).add(0); tree.get(1).add(2); tree.get(2).add(1); tree.get(2).add(3); tree.get(3).add(2); dfs(0, -1); System.out.println(diameter); } }
Attempts:
2 left
💡 Hint
Check the size of the tree list and the indices accessed.
✗ Incorrect
The code initializes tree with size 3 but tries to access tree.get(3), which is out of bounds causing IndexOutOfBoundsException.
❓ Predict Output
advancedOutput of Diameter Calculation on a Star-Shaped Tree
What is the output of the following Java code that calculates the diameter of a star-shaped tree with 5 nodes?
DSA Java
import java.util.*; public class TreeDiameter { static List<List<Integer>> tree = new ArrayList<>(); static int diameter = 0; public static int dfs(int node, int parent) { int max1 = 0, max2 = 0; for (int child : tree.get(node)) { if (child == parent) continue; int height = dfs(child, node); if (height > max1) { max2 = max1; max1 = height; } else if (height > max2) { max2 = height; } } diameter = Math.max(diameter, max1 + max2); return max1 + 1; } public static void main(String[] args) { int n = 5; for (int i = 0; i < n; i++) tree.add(new ArrayList<>()); tree.get(0).add(1); tree.get(1).add(0); tree.get(0).add(2); tree.get(2).add(0); tree.get(0).add(3); tree.get(3).add(0); tree.get(0).add(4); tree.get(4).add(0); dfs(0, -1); System.out.println(diameter); } }
Attempts:
2 left
💡 Hint
In a star-shaped tree, the longest path is between two leaf nodes through the center.
✗ Incorrect
The diameter is the longest path between two leaves through the center node 0, which has length 2 edges.
🚀 Application
expertMaximum Diameter After Adding One Edge
Given a tree with 6 nodes and edges [[0,1],[1,2],[1,3],[3,4],[3,5]], if you add one edge connecting any two existing nodes (not already connected), what is the maximum possible diameter of the resulting graph?
Attempts:
2 left
💡 Hint
Adding an edge can create a cycle and may reduce the diameter.
✗ Incorrect
The original tree diameter is 3 (path from node 2 to node 4 or 5). Adding an edge creates a cycle, so the diameter cannot increase beyond original diameter. The maximum diameter remains 3.
