Challenge - 5 Problems
Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Diameter Calculation on Simple Tree
What is the output of the following C# code that calculates the diameter of a binary tree?
DSA C#
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Solution { private int maxDiameter = 0; public int DiameterOfBinaryTree(TreeNode root) { Depth(root); return maxDiameter; } private int Depth(TreeNode node) { if (node == null) return 0; int left = Depth(node.left); int right = Depth(node.right); maxDiameter = Math.Max(maxDiameter, left + right); return Math.Max(left, right) + 1; } } // Tree structure: // 1 // / \ // 2 3 // / // 4 var root = new TreeNode(1, new TreeNode(2, new TreeNode(4), null), new TreeNode(3)); var sol = new Solution(); System.Console.WriteLine(sol.DiameterOfBinaryTree(root));
Attempts:
2 left
💡 Hint
The diameter is the longest path between any two nodes, counting edges.
✗ Incorrect
The longest path goes from node 4 to node 3 through nodes 2 and 1, which has 3 edges.
❓ Predict Output
intermediateDiameter Output for Skewed Tree
What will be the output of the diameter calculation for this skewed binary tree?
DSA C#
// Tree structure: // 1 // \ // 2 // \ // 3 // \ // 4 var root = new TreeNode(1, null, new TreeNode(2, null, new TreeNode(3, null, new TreeNode(4)))); var sol = new Solution(); System.Console.WriteLine(sol.DiameterOfBinaryTree(root));
Attempts:
2 left
💡 Hint
Diameter counts edges, so count edges along the longest path.
✗ Incorrect
The longest path is from node 1 to node 4, which has 3 edges.
🔧 Debug
advancedIdentify the Error in Diameter Calculation Code
What error will this code produce when calculating the diameter of a binary tree?
DSA C#
public class Solution { private int maxDiameter = 0; public int DiameterOfBinaryTree(TreeNode root) { Depth(root); return maxDiameter; } private int Depth(TreeNode node) { if (node == null) return 0; int left = Depth(node.left); int right = Depth(node.right); maxDiameter = Math.Max(maxDiameter, left * right); return Math.Max(left, right) + 1; } }
Attempts:
2 left
💡 Hint
Check how maxDiameter is updated inside Depth method.
✗ Incorrect
The code uses multiplication (left * right) instead of addition (left + right), so diameter calculation is wrong.
🧠 Conceptual
advancedUnderstanding Diameter Definition
Which statement 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.
❓ Predict Output
expertDiameter Calculation on Complex Tree
What is the output of the diameter calculation on this binary tree?
DSA C#
// Tree structure: // 1 // / \ // 2 3 // / / \ // 4 5 6 // / \ // 7 8 // \ // 9 var root = new TreeNode(1, new TreeNode(2, new TreeNode(4), null), new TreeNode(3, new TreeNode(5, new TreeNode(7), null), new TreeNode(6, null, new TreeNode(8, null, new TreeNode(9))))); var sol = new Solution(); System.Console.WriteLine(sol.DiameterOfBinaryTree(root));
Attempts:
2 left
💡 Hint
Trace the longest path between two nodes counting edges.
✗ Incorrect
The longest path is from node 4 to node 9: 4 -> 2 -> 1 -> 3 -> 6 -> 8 -> 9, which has 6 edges.
