Challenge - 5 Problems
Diameter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of diameter calculation on a simple binary tree
What is the output of the following code that calculates the diameter of a binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def diameter(root): diameter_result = [0] def depth(node): if not node: return 0 left = depth(node.left) right = depth(node.right) diameter_result[0] = max(diameter_result[0], left + right) return 1 + max(left, right) depth(root) return diameter_result[0] root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) print(diameter(root))
Attempts:
2 left
💡 Hint
Think about the longest path between any two nodes in the tree.
✗ Incorrect
The diameter is the longest path between any two nodes. Here, the longest path goes through nodes 4 -> 2 -> 1 -> 3, which has length 3 edges.
🧠 Conceptual
intermediateUnderstanding diameter definition in binary trees
Which of the following best describes the diameter of a binary tree?
Attempts:
2 left
💡 Hint
Diameter counts edges, not nodes.
✗ Incorrect
The diameter is defined as the number of edges on the longest path between any two nodes, not the number of nodes.
🔧 Debug
advancedIdentify the error in diameter calculation code
What error will the following code produce when calculating the diameter of a binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def diameter(root): max_diameter = 0 def depth(node): if not node: return 0 left = depth(node.left) right = depth(node.right) nonlocal max_diameter max_diameter = max(max_diameter, left + right) return 1 + max(left, right) depth(root) return max_diameter root = Node(1) root.left = Node(2) root.right = Node(3) print(diameter(root))
Attempts:
2 left
💡 Hint
Check variable scope inside nested functions.
✗ Incorrect
The variable max_diameter is assigned inside the nested function without 'nonlocal', so the outer max_diameter is never updated. The function returns 0.
🚀 Application
advancedDiameter of unbalanced binary tree
Given the following unbalanced binary tree, what is the diameter?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(1) root.left = Node(2) root.left.left = Node(3) root.left.left.left = Node(4) root.right = Node(5) root.right.right = Node(6) root.right.right.right = Node(7) # Diameter function as defined previously def diameter(root): diameter_result = [0] def depth(node): if not node: return 0 left = depth(node.left) right = depth(node.right) diameter_result[0] = max(diameter_result[0], left + right) return 1 + max(left, right) depth(root) return diameter_result[0] print(diameter(root))
Attempts:
2 left
💡 Hint
Consider the longest path between leaf nodes across the root.
✗ Incorrect
The longest path is from node 4 to node 7 passing through root: length 6 edges.
🧠 Conceptual
expertTime complexity of diameter calculation using single traversal
What is the time complexity of calculating the diameter of a binary tree using a single traversal approach that computes depth and updates diameter simultaneously?
Attempts:
2 left
💡 Hint
Each node is visited once in the single traversal method.
✗ Incorrect
The single traversal method visits each node once, so the time complexity is linear O(n).
