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 Python code that calculates the diameter of a tree represented as an adjacency list?
DSA Python
def tree_diameter(adj): n = len(adj) def dfs(node, parent): max1 = max2 = 0 for nei in adj[node]: if nei == parent: continue length = dfs(nei, node) + 1 if length > max1: max2 = max1 max1 = length elif length > max2: max2 = length tree_diameter.max_diameter = max(tree_diameter.max_diameter, max1 + max2) return max1 tree_diameter.max_diameter = 0 dfs(0, -1) return tree_diameter.max_diameter adjacency = [[1,2],[0,3,4],[0],[1],[1]] print(tree_diameter(adjacency))
Attempts:
2 left
💡 Hint
Trace the longest path between any two nodes in the tree.
✗ Incorrect
The longest path (diameter) in the tree is between node 3 and node 2 or node 4 and node 2, which has length 3 edges.
🧠 Conceptual
intermediateUnderstanding the role of two longest paths in diameter calculation
In the DP approach to find the diameter of a tree, why do we keep track of the two longest paths from a node to its children?
Attempts:
2 left
💡 Hint
Think about how the diameter can pass through a node connecting two subtrees.
✗ Incorrect
The diameter can be the sum of the two longest downward paths from a node, passing through that node, so tracking both helps find the maximum diameter.
🔧 Debug
advancedIdentify the error in diameter calculation code
What error will the following code produce when calculating the diameter of a tree?
DSA Python
def diameter(adj): def dfs(u, p): max_len = 0 for v in adj[u]: if v == p: continue length = dfs(v, u) + 1 max_len = max(max_len, length) diameter.max_d = max(diameter.max_d, max_len) return max_len diameter.max_d = 0 dfs(0, -1) return diameter.max_d adj = [[1],[0,2],[1]] print(diameter(adj))
Attempts:
2 left
💡 Hint
Check how the diameter is updated inside the DFS.
✗ Incorrect
The code only tracks the longest single path from a node, missing the sum of two longest paths needed for diameter, so it outputs 1 instead of 2.
❓ Predict Output
advancedOutput of diameter on a star-shaped tree
What is the output of the diameter function on this star-shaped tree adjacency list?
DSA Python
def diameter(adj): def dfs(u, p): max1 = max2 = 0 for v in adj[u]: if v == p: continue length = dfs(v, u) + 1 if length > max1: max2 = max1 max1 = length elif length > max2: max2 = length diameter.max_d = max(diameter.max_d, max1 + max2) return max1 diameter.max_d = 0 dfs(0, -1) return diameter.max_d adjacency = [[1,2,3,4],[0],[0],[0],[0]] print(diameter(adjacency))
Attempts:
2 left
💡 Hint
The diameter in a star tree is the longest path between two leaves through the center.
✗ Incorrect
The longest path is between any two leaves through the center node, which has length 2 edges.
🚀 Application
expertCalculate diameter after adding an edge
Given a tree with adjacency list below, what will be the diameter after adding an edge between nodes 3 and 5?
DSA Python
def diameter(adj): def dfs(u, p): max1 = max2 = 0 for v in adj[u]: if v == p: continue length = dfs(v, u) + 1 if length > max1: max2 = max1 max1 = length elif length > max2: max2 = length diameter.max_d = max(diameter.max_d, max1 + max2) return max1 diameter.max_d = 0 dfs(0, -1) return diameter.max_d adjacency = [[1,2],[0,3],[0,4],[1],[2,5],[4]] # Edge between 3 and 5 is added adjacency[3].append(5) adjacency[5].append(3) print(diameter(adjacency))
Attempts:
2 left
💡 Hint
Adding the edge creates a cycle, but diameter is still longest simple path.
✗ Incorrect
The added edge creates a cycle, but the longest simple path (diameter) increases to 5 edges.
