Challenge - 5 Problems
Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function diameterOfBinaryTree(root) { let diameter = 0; function depth(node) { if (!node) return 0; const left = depth(node.left); const right = depth(node.right); diameter = Math.max(diameter, left + right); return Math.max(left, right) + 1; } depth(root); return diameter; } const root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3)); console.log(diameterOfBinaryTree(root));
Attempts:
2 left
💡 Hint
The diameter is the longest path between any two nodes, counting edges.
✗ Incorrect
The longest path goes through nodes 4 -> 2 -> 1 -> 3 or 5 -> 2 -> 1 -> 3, which has 3 edges.
❓ Predict Output
intermediate2:00remaining
Diameter of a skewed binary tree
What is the output of the diameterOfBinaryTree function for this skewed tree?
DSA Javascript
const root = new TreeNode(1, new TreeNode(2, new TreeNode(3, new TreeNode(4)))); console.log(diameterOfBinaryTree(root));
Attempts:
2 left
💡 Hint
In a skewed tree, the diameter is the number of edges in the longest chain.
✗ Incorrect
The tree is a straight line with 4 nodes, so the diameter is 3 edges.
🔧 Debug
advanced2:00remaining
Identify the error in this diameter calculation code
What error will this code produce when calculating the diameter of a binary tree?
DSA Javascript
function diameterOfBinaryTree(root) {
let diameter = 0;
function depth(node) {
if (node === null) return 0;
const left = depth(node.left);
const right = depth(node.right);
diameter = Math.max(diameter, left + right + 1);
return Math.max(left, right) + 1;
}
depth(root);
return diameter;
}Attempts:
2 left
💡 Hint
Diameter counts edges, but code adds 1 extra edge.
✗ Incorrect
The code adds 1 to left + right, counting nodes instead of edges, so the diameter is one too large.
🧠 Conceptual
advanced2:00remaining
Understanding diameter calculation logic
Why does the diameter calculation use the sum of left and right subtree depths at each node?
Attempts:
2 left
💡 Hint
Think about the longest path crossing the root of a subtree.
✗ Incorrect
The diameter can be the longest path passing through a node, which is the sum of depths of left and right subtrees.
❓ Predict Output
expert2:00remaining
Diameter of a complex binary tree with multiple branches
What is the output of the diameterOfBinaryTree function for this tree?
DSA Javascript
const root = new TreeNode(1, new TreeNode(2, new TreeNode(4, new TreeNode(7), null ), new TreeNode(5) ), new TreeNode(3, null, new TreeNode(6, new TreeNode(8), new TreeNode(9) ) ) ); console.log(diameterOfBinaryTree(root));
Attempts:
2 left
💡 Hint
Trace the longest path between two leaves in different branches.
✗ Incorrect
The longest path is 6 edges long, for example from node 7 to node 9 passing through nodes 4, 2, 1, 3, and 6.