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 Go code that calculates the diameter of a tree represented as an adjacency list?
DSA Go
package main import "fmt" var ( graph = [][]int{{1, 2}, {0, 3, 4}, {0}, {1}, {1}} maxDiameter int ) func dfs(node, parent int) int { max1, max2 := 0, 0 for _, child := range graph[node] { if child == parent { continue } length := dfs(child, node) + 1 if length > max1 { max2 = max1 max1 = length } else if length > max2 { max2 = length } } if max1+max2 > maxDiameter { maxDiameter = max1 + max2 } return max1 } func main() { dfs(0, -1) fmt.Println(maxDiameter) }
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. The longest path here is from node 2 to node 3 or 4 through node 0 and 1, which has length 3 edges.
🧠 Conceptual
intermediateUnderstanding Diameter Calculation Logic
In the diameter calculation using DP on trees, why do we keep track of the two longest paths from a node's children?
Attempts:
2 left
💡 Hint
Diameter can pass through a node connecting two longest child paths.
✗ Incorrect
The diameter may pass through a node connecting two longest paths from its children, so tracking the top two longest paths helps find the maximum diameter.
🔧 Debug
advancedIdentify the Bug in Diameter Calculation Code
What error will the following Go code produce when calculating the diameter of a tree?
DSA Go
package main import "fmt" var ( graph = [][]int{{1}, {0, 2}, {1}} maxDiameter int ) func dfs(node int, parent int) int { max1, max2 := 0, 0 for _, child := range graph[node] { if child == parent { continue } length := dfs(child, node) + 1 if length > max1 { max2 = max1 max1 = length } else if length > max2 { max2 = length } } if max1+max2 > maxDiameter { maxDiameter = max1 + max2 } return max1 } func main() { dfs(0, -1) fmt.Println(maxDiameter) }
Attempts:
2 left
💡 Hint
Check the base case or the condition to skip the parent node.
✗ Incorrect
The condition 'if child == node' is wrong; it should be 'if child == parent' to avoid revisiting the parent and causing infinite recursion.
❓ Predict Output
advancedDiameter Output for a Star-Shaped Tree
What is the output of the diameter calculation for the following star-shaped tree?
DSA Go
package main import "fmt" var ( graph = [][]int{{1, 2, 3, 4}, {0}, {0}, {0}, {0}} maxDiameter int ) func dfs(node, parent int) int { max1, max2 := 0, 0 for _, child := range graph[node] { if child == parent { continue } length := dfs(child, node) + 1 if length > max1 { max2 = max1 max1 = length } else if length > max2 { max2 = length } } if max1+max2 > maxDiameter { maxDiameter = max1 + max2 } return max1 } func main() { dfs(0, -1) fmt.Println(maxDiameter) }
Attempts:
2 left
💡 Hint
The diameter is the longest path between two leaf nodes through the center.
✗ Incorrect
In a star-shaped tree, the longest path is between any two leaves through the center node, which has length 2 edges.
🚀 Application
expertMaximum Diameter After Adding One Edge
Given a tree with 5 nodes and edges [[0,1],[1,2],[1,3],[3,4]], what is the maximum possible diameter after adding exactly one new edge between any two nodes?
Attempts:
2 left
💡 Hint
Adding an edge can create a cycle and potentially increase the longest path.
✗ Incorrect
Original diameter is 3 (path 2-1-3-4). Adding an edge between nodes 2 and 4 creates a cycle and a longer path of length 4.
