Bird
Raised Fist0
DSA Cprogramming~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 for Simple Tree
What is the output of the following C code that calculates the diameter of a binary tree?
DSA C
typedef struct Node {
    int val;
    struct Node* left;
    struct Node* right;
} Node;

int max(int a, int b) {
    return (a > b) ? a : b;
}

int height(Node* root) {
    if (!root) return 0;
    return 1 + max(height(root->left), height(root->right));
}

int diameter(Node* root) {
    if (!root) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    int leftDiameter = diameter(root->left);
    int rightDiameter = diameter(root->right);
    int maxDiameter = max(leftDiameter, rightDiameter);
    return max(leftHeight + rightHeight + 1, maxDiameter);
}

int main() {
    Node n4 = {4, NULL, NULL};
    Node n5 = {5, NULL, NULL};
    Node n2 = {2, &n4, &n5};
    Node n3 = {3, NULL, NULL};
    Node n1 = {1, &n2, &n3};
    int d = diameter(&n1);
    printf("%d\n", d);
    return 0;
}
A4
B5
C3
D6
Attempts:
2 left
💡 Hint
Think about the longest path between two nodes in the tree.
❓ Predict Output
intermediate
2:00remaining
Diameter Output for Unbalanced Tree
What is the output of the diameter function for this unbalanced binary tree?
DSA C
typedef struct Node {
    int val;
    struct Node* left;
    struct Node* right;
} Node;

int max(int a, int b) {
    return (a > b) ? a : b;
}

int height(Node* root) {
    if (!root) return 0;
    return 1 + max(height(root->left), height(root->right));
}

int diameter(Node* root) {
    if (!root) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    int leftDiameter = diameter(root->left);
    int rightDiameter = diameter(root->right);
    int maxDiameter = max(leftDiameter, rightDiameter);
    return max(leftHeight + rightHeight + 1, maxDiameter);
}

int main() {
    Node n5 = {5, NULL, NULL};
    Node n4 = {4, &n5, NULL};
    Node n3 = {3, &n4, NULL};
    Node n2 = {2, &n3, NULL};
    Node n1 = {1, &n2, NULL};
    int d = diameter(&n1);
    printf("%d\n", d);
    return 0;
}
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
The tree is a straight line, so diameter equals the number of nodes.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Diameter Calculation
The following code aims to calculate the diameter of a binary tree but produces incorrect results. What is the main bug?
DSA C
typedef struct Node {
    int val;
    struct Node* left;
    struct Node* right;
} Node;

int max(int a, int b) {
    return (a > b) ? a : b;
}

int height(Node* root) {
    if (!root) return 0;
    return 1 + max(height(root->left), height(root->right));
}

int diameter(Node* root) {
    if (!root) return 0;
    int leftHeight = height(root->left);
    int rightHeight = height(root->right);
    int leftDiameter = diameter(root->left);
    int rightDiameter = diameter(root->right);
    int maxDiameter = max(leftDiameter, rightDiameter);
    return max(leftHeight + rightHeight, maxDiameter);
}
AThe diameter function does not check for NULL root nodes.
BThe diameter calculation misses adding 1 for the current node in the path length.
CThe max function is incorrectly implemented and returns the smaller value.
DThe height function returns 0 for leaf nodes instead of 1.
Attempts:
2 left
💡 Hint
Check how the diameter is computed from heights of left and right subtrees.
🚀 Application
advanced
2:00remaining
Calculate Diameter for Given Tree Structure
Given the binary tree below, what is the diameter? Tree structure: - Root node 1 - Left child 2 with left child 4 and right child 5 - Right child 3 with right child 6 - Node 6 has left child 7 Use the diameter definition as the longest path between any two nodes.
A7
B5
C6
D8
Attempts:
2 left
💡 Hint
Consider the longest path that may pass through the root or deeper nodes.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Diameter Calculation
What is the time complexity of the naive diameter calculation algorithm that uses separate height calculations for each node?
AO(n^2) because height is calculated repeatedly for each node
BO(n) because each node is visited once
CO(n log n) because height calculation is logarithmic
DO(log n) because the tree height is logarithmic
Attempts:
2 left
💡 Hint
Consider how many times height is called for each node.

Practice

(1/5)
1. What does the diameter of a binary tree represent?
easy
A. The total number of nodes in the tree
B. The height of the tree
C. The length of the longest path between any two 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 longest path from root to leaf, leaf count is number of leaves, total nodes is size. Diameter is distinct.
  3. Final Answer:

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

    Diameter = longest path length [OK]
Hint: Diameter is longest path, not height or node count [OK]
Common Mistakes:
  • Confusing diameter with height
  • Thinking diameter counts nodes instead of edges
  • Assuming diameter always passes through root
2. Which of the following C function signatures is correct for a helper function that returns the height of a binary tree node for diameter calculation?
easy
A. int height(struct Node* root);
B. void height(struct Node root);
C. int height(Node root);
D. int height(struct Node root);

Solution

  1. Step 1: Check parameter type for tree node

    In C, tree nodes are passed as pointers to struct Node, so parameter should be struct Node* root.
  2. Step 2: Check return type and parameter correctness

    Height returns int, so return type int is correct. Passing pointer is correct, not struct by value.
  3. Final Answer:

    int height(struct Node* root); -> Option A
  4. Quick Check:

    Pointer parameter and int return type [OK]
Hint: Use pointer to struct Node for tree nodes in C [OK]
Common Mistakes:
  • Passing struct Node by value instead of pointer
  • Using void return type instead of int
  • Missing struct keyword in parameter
3. What is the output of the following C code snippet that calculates the diameter of a binary tree?
struct Node {
  int data;
  struct Node* left;
  struct Node* right;
};

int height(struct Node* root) {
  if (!root) return 0;
  int leftHeight = height(root->left);
  int rightHeight = height(root->right);
  return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

int diameter(struct Node* root) {
  if (!root) return 0;
  int leftHeight = height(root->left);
  int rightHeight = height(root->right);
  int leftDiameter = diameter(root->left);
  int rightDiameter = diameter(root->right);
  int maxDiameter = leftHeight + rightHeight + 1;
  if (leftDiameter > maxDiameter) maxDiameter = leftDiameter;
  if (rightDiameter > maxDiameter) maxDiameter = rightDiameter;
  return maxDiameter;
}

int main() {
  struct Node n1 = {1, NULL, NULL};
  struct Node n2 = {2, NULL, NULL};
  struct Node n3 = {3, &n1, &n2};
  printf("%d", diameter(&n3));
  return 0;
}
medium
A. 1
B. 2
C. 4
D. 3

Solution

  1. Step 1: Analyze tree structure

    Root n3 has two children n1 and n2, both leaves. Height of left and right subtree is 1 each.
  2. Step 2: Calculate diameter

    Diameter = max of (leftHeight + rightHeight + 1, leftDiameter, rightDiameter) = max(1+1+1, 1, 1) = 3.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Diameter = 3 nodes in longest path [OK]
Hint: Diameter = max(leftHeight + rightHeight + 1, leftDiameter, rightDiameter) [OK]
Common Mistakes:
  • Confusing diameter with height
  • Forgetting to add 1 for root node in diameter
  • Using height instead of diameter in comparisons
4. Identify the bug in this C function that attempts to compute the diameter of a binary tree:
int diameter(struct Node* root) {
  if (!root) return 0;
  int leftHeight = height(root->left);
  int rightHeight = height(root->right);
  int leftDiameter = diameter(root->left);
  int rightDiameter = diameter(root->right);
  int maxDiameter = leftHeight + rightHeight;
  if (leftDiameter > maxDiameter) maxDiameter = leftDiameter;
  if (rightDiameter > maxDiameter) maxDiameter = rightDiameter;
  return maxDiameter;
}
medium
A. height function is not called correctly
B. maxDiameter should be leftHeight + rightHeight + 1
C. Function should return height, not diameter
D. Missing base case for leaf nodes

Solution

  1. Step 1: Check diameter calculation formula

    Diameter includes edges between nodes, so it should add 1 for the root node connecting left and right paths.
  2. Step 2: Identify missing +1 in maxDiameter

    Current code misses +1, so maxDiameter undercounts by 1.
  3. Final Answer:

    maxDiameter should be leftHeight + rightHeight + 1 -> Option B
  4. Quick Check:

    Diameter = leftHeight + rightHeight + 1 [OK]
Hint: Add 1 to sum of heights for diameter at root [OK]
Common Mistakes:
  • Omitting +1 in diameter calculation
  • Confusing height and diameter returns
  • Not handling null root base case
5. Given a binary tree where each node stores an integer, how would you modify the diameter calculation to find the diameter based on the sum of node values along the longest path instead of the number of nodes?
hard
A. Change height function to return max sum of node values from root to leaf, and update diameter to sum left and right max sums plus root's value
B. Use the existing diameter function but multiply the result by the root node's value
C. Replace all node values with 1 and then calculate diameter normally
D. Calculate diameter normally and then add all node values in the tree separately

Solution

  1. Step 1: Modify height to return max sum path

    Instead of counting nodes, height returns max sum of node values from root to leaf recursively.
  2. Step 2: Update diameter to sum left and right max sums plus root value

    Diameter becomes max of leftDiameter, rightDiameter, and leftSum + rightSum + root's value.
  3. Final Answer:

    Change height function to return max sum of node values from root to leaf, and update diameter to sum left and right max sums plus root's value -> Option A
  4. Quick Check:

    Diameter by sum = max path sum through root [OK]
Hint: Use max sum path instead of node count for diameter sum [OK]
Common Mistakes:
  • Multiplying diameter by root value incorrectly
  • Ignoring node values in diameter calculation
  • Adding all node values separately instead of path sums
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