Bird
Raised Fist0
DSA C#programming~10 mins

Diameter of Binary Tree in DSA C# - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the TreeNode class with left and right children.

DSA C#
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int x) { val = x; [1]; }
}
Drag options to blanks, or click blank then click option'
Aleft = right = null
Bleft = right = 0
Cleft = right = new TreeNode(0)
Dleft = right = this
Attempts:
3 left
💡 Hint
Common Mistakes
Setting left and right to 0 instead of null.
Creating new nodes inside the constructor unnecessarily.
2fill in blank
medium

Complete the code to calculate the height of a binary tree node recursively.

DSA C#
private int Height(TreeNode node) {
    if (node == null) return 0;
    int leftHeight = Height(node.left);
    int rightHeight = Height(node.right);
    return 1 + [1];
}
Drag options to blanks, or click blank then click option'
AMath.Max(leftHeight, rightHeight)
BleftHeight + rightHeight
CMath.Min(leftHeight, rightHeight)
DleftHeight - rightHeight
Attempts:
3 left
💡 Hint
Common Mistakes
Adding left and right heights instead of taking max.
Using Math.Min which gives the shorter subtree.
3fill in blank
hard

Fix the error in updating the diameter during recursion.

DSA C#
private int DiameterHelper(TreeNode node, ref int diameter) {
    if (node == null) return 0;
    int leftHeight = DiameterHelper(node.left, ref diameter);
    int rightHeight = DiameterHelper(node.right, ref diameter);
    diameter = Math.Max(diameter, [1]);
    return 1 + Math.Max(leftHeight, rightHeight);
}
Drag options to blanks, or click blank then click option'
AleftHeight + rightHeight + 1
BleftHeight + rightHeight
CMath.Max(leftHeight, rightHeight)
DMath.Min(leftHeight, rightHeight)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to the sum which counts nodes instead of edges.
Using max or min of heights instead of sum.
4fill in blank
hard

Fill both blanks to complete the DiameterOfBinaryTree method using the helper.

DSA C#
public int DiameterOfBinaryTree(TreeNode root) {
    int diameter = 0;
    [1](root, ref [2]);
    return diameter;
}
Drag options to blanks, or click blank then click option'
ADiameterHelper
Bdiameter
CHeight
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Height instead of DiameterHelper.
Passing root instead of diameter by reference.
5fill in blank
hard

Fill all three blanks to complete the recursive DiameterHelper method.

DSA C#
private int DiameterHelper(TreeNode node, ref int diameter) {
    if (node == null) return 0;
    int leftHeight = DiameterHelper(node.[1], ref diameter);
    int rightHeight = DiameterHelper(node.[2], ref diameter);
    diameter = Math.Max(diameter, leftHeight [3] rightHeight);
    return 1 + Math.Max(leftHeight, rightHeight);
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right for both left and right heights.
Using subtraction instead of addition for diameter update.

Practice

(1/5)
1. What does the diameter of a binary tree represent?
easy
A. The length of the longest path between any two nodes in the tree
B. The height of the tree from root to the deepest leaf
C. The total number of nodes in the tree
D. The number of leaf nodes in the tree

Solution

  1. Step 1: Understand the diameter concept

    The diameter is the longest path between any two nodes, which may or may not pass through the root.
  2. Step 2: Differentiate from height and node count

    Height is the longest path from root to leaf, node count is total nodes, and leaf count is number of leaves, none of which define diameter.
  3. Final Answer:

    The length of the longest path between any two nodes in the tree -> Option A
  4. Quick Check:

    Diameter = longest path between nodes [OK]
Hint: Diameter is longest path, not just height or node count [OK]
Common Mistakes:
  • Confusing diameter with tree height
  • Thinking diameter counts nodes instead of edges
  • Assuming diameter must pass through root
2. Which of the following C# method signatures is correct for a function that calculates the diameter of a binary tree node?
easy
A. Diameter public int(TreeNode root)
B. int Diameter(TreeNode root) void
C. public void Diameter(TreeNode root) int
D. public int Diameter(TreeNode root)

Solution

  1. Step 1: Review C# method signature syntax

    In C#, method signature starts with access modifier, return type, method name, and parameters in parentheses.
  2. Step 2: Check each option

    public int Diameter(TreeNode root) follows correct syntax: public int Diameter(TreeNode root). Others have incorrect order or missing parts.
  3. Final Answer:

    public int Diameter(TreeNode root) -> Option D
  4. Quick Check:

    Correct C# method syntax = public int Diameter(TreeNode root) [OK]
Hint: Method signature: access modifier + return type + name + parameters [OK]
Common Mistakes:
  • Placing return type after parameters
  • Omitting access modifier or return type
  • Incorrect order of keywords
3. Given the following binary tree code snippet, what is the diameter of the tree?
class TreeNode {
  public int val;
  public TreeNode left, right;
  public TreeNode(int v) { val = v; }
}

// Tree structure:
//       1
//      / \
//     2   3
//    / 
//   4

TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);

int diameter = CalculateDiameter(root); // Assume CalculateDiameter correctly implemented
Console.WriteLine(diameter);
medium
A. 3
B. 2
C. 4
D. 5

Solution

  1. Step 1: Visualize the tree and longest path

    The longest path is from node 4 to node 3: path length = edges 4->2->1->3 = 3 edges.
  2. Step 2: Calculate diameter as number of edges

    Diameter counts edges, so longest path length is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Longest path edges = 3 [OK]
Hint: Count edges on longest path, not nodes [OK]
Common Mistakes:
  • Counting nodes instead of edges
  • Assuming diameter passes through root only
  • Ignoring one side of the tree
4. Identify the bug in this C# code snippet for calculating the diameter of a binary tree:
int maxDiameter = 0;
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 + 1);
  return Math.Max(left, right) + 1;
}

int Diameter(TreeNode root) {
  Depth(root);
  return maxDiameter;
}
medium
A. The base case should return 1 instead of 0
B. Diameter should be maxDiameter - 1 because diameter counts edges, not nodes
C. maxDiameter should be reset inside Depth method
D. Depth method should return left + right instead of max

Solution

  1. Step 1: Understand diameter counting edges vs nodes

    The code adds left + right + 1 (nodes), but diameter counts edges, so subtract 1.
  2. Step 2: Fix return value in Diameter method

    Return maxDiameter - 1 to convert node count to edge count for diameter.
  3. Final Answer:

    Diameter should be maxDiameter - 1 because diameter counts edges, not nodes -> Option B
  4. Quick Check:

    Diameter edges = nodes - 1 [OK]
Hint: Diameter = nodes on path - 1 (edges) [OK]
Common Mistakes:
  • Returning maxDiameter directly without subtracting 1
  • Changing base case return value incorrectly
  • Resetting maxDiameter inside recursive calls
5. You want to find the diameter of a binary tree but also return the path nodes forming that diameter. Which approach correctly extends the diameter calculation?
hard
A. Calculate diameter first, then do a separate BFS to find the path
B. Store all paths from root to leaves and pick the longest one
C. Modify the recursive function to return both depth and path, updating max path when diameter increases
D. Use a queue to traverse level by level and count nodes

Solution

  1. Step 1: Understand diameter and path relationship

    Diameter is longest path between any two nodes, so tracking path during recursion helps capture it.
  2. Step 2: Extend recursion to return depth and path

    Modify recursive calls to return depth and path list; update max path when a longer diameter is found.
  3. Step 3: Compare other options

    Calculate diameter first, then do a separate BFS to find the path is inefficient; C only finds longest root-to-leaf path, not diameter; D is unrelated to diameter.
  4. Final Answer:

    Modify the recursive function to return both depth and path, updating max path when diameter increases -> Option C
  5. Quick Check:

    Track depth and path together for diameter path [OK]
Hint: Return depth and path in recursion to track diameter path [OK]
Common Mistakes:
  • Trying to find path after diameter without tracking during recursion
  • Confusing longest root-to-leaf path with diameter
  • Using BFS level order traversal which doesn't find diameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard