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 Simple Tree
What is the output of the following TypeScript code that calculates the diameter of a binary tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function diameterOfBinaryTree(root: TreeNode | null): number { let diameter = 0; function depth(node: TreeNode | null): number { 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 tree = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3)); console.log(diameterOfBinaryTree(tree));
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 4 to node 3 through nodes 2 and 1, which has length 3 edges.
🧠 Conceptual
intermediate1:30remaining
Understanding Diameter Definition
Which of the following best describes the diameter of a binary tree?
Attempts:
2 left
💡 Hint
Diameter counts edges, not nodes.
✗ Incorrect
Diameter is defined as the number of edges on the longest path between any two nodes in the tree.
🔧 Debug
advanced2:00remaining
Identify the Bug in Diameter Calculation
What error will the following TypeScript code produce when calculating the diameter of a binary tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function diameterOfBinaryTree(root: TreeNode | null): number { let diameter = 0; function depth(node: TreeNode | null): number { 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); } depth(root); return diameter; } const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(diameterOfBinaryTree(tree));
Attempts:
2 left
💡 Hint
Check the return statement of the depth function.
✗ Incorrect
The depth function should return max(left, right) + 1 to count the current node. Missing +1 causes incorrect depth and diameter calculation.
🚀 Application
advanced2:00remaining
Diameter of Unbalanced Binary Tree
Given the following unbalanced binary tree, what is the diameter?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } const tree = new TreeNode(1, new TreeNode(2, new TreeNode(3, new TreeNode(4), null), null), new TreeNode(5));
Attempts:
2 left
💡 Hint
Find the longest path between two nodes in the tree.
✗ Incorrect
The longest path is from node 4 to node 5 through nodes 3, 2, and 1, which has 4 edges.
❓ Predict Output
expert2:30remaining
Output of Diameter Calculation with Multiple Paths
What is the output of the following TypeScript code that calculates the diameter of a binary tree with multiple longest paths?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function diameterOfBinaryTree(root: TreeNode | null): number { let diameter = 0; function depth(node: TreeNode | null): number { 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 tree = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, null, new TreeNode(6, new TreeNode(7), null)) ); console.log(diameterOfBinaryTree(tree));
Attempts:
2 left
💡 Hint
Consider all longest paths and count edges.
✗ Incorrect
The longest path is from node 7 to node 5 through nodes 6, 3, 1, and 2, which has 5 edges.