Bird
Raised Fist0
DSA C#programming~20 mins

Diameter of Binary Tree in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Diameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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));
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
The diameter is the longest path between any two nodes, counting edges.
❓ Predict Output
intermediate
2:00remaining
Diameter 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));
A0
B4
C1
D3
Attempts:
2 left
💡 Hint
Diameter counts edges, so count edges along the longest path.
🔧 Debug
advanced
2:00remaining
Identify 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;
    }
}
ACompilation error due to missing return statement
BThe diameter will be the product of left and right depths, which is incorrect
CStackOverflowException due to infinite recursion
DThe diameter will always be zero
Attempts:
2 left
💡 Hint
Check how maxDiameter is updated inside Depth method.
🧠 Conceptual
advanced
1:30remaining
Understanding Diameter Definition
Which statement best describes the diameter of a binary tree?
AThe number of nodes on the longest path between any two nodes
BThe height of the tree plus one
CThe number of edges on the longest path between any two nodes
DThe number of leaf nodes in the tree
Attempts:
2 left
💡 Hint
Diameter counts edges, not nodes.
❓ Predict Output
expert
2:30remaining
Diameter 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));
A6
B8
C7
D9
Attempts:
2 left
💡 Hint
Trace the longest path between two nodes counting edges.

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