DSA Series Chapter 24 – DIAMETER OF BINARY TREE(C Version)


JOIN for daily coding insights

DSA Series Chapter 24 – DIAMETER OF BINARY TREE(C Version)


1. Objective

To compute the diameter of a binary tree using a recursive approach in C.


2. Definition (Exam-Ready)

The diameter of a binary tree is the number of nodes on the longest path between any two leaf nodes in the tree.

* The path may or may not pass through the root.


3. Example Binary Tree

            1
          /   \
         2     3
        / \
       4   5

Longest Path

4 → 2 → 1 → 3

Diameter

  • Nodes in path = 4

  • Diameter = 4


4. Important Observation

For every node:

  • Longest path through that node =
    height(left subtree) + height(right subtree) + 1

The maximum such value over all nodes is the diameter.


5. Core Logic

  • Compute height of left subtree

  • Compute height of right subtree

  • Diameter at current node = lh + rh + 1

  • Recursively compute diameter of left subtree

  • Recursively compute diameter of right subtree

  • Return the maximum of all three


6. Recursive Formula

diameter(node) =
    max(
        height(left) + height(right) + 1,
        diameter(left),
        diameter(right)
    )

7. Structure Definition

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

8. Function to Find Height

int height(struct Node* root) {
    if (root == NULL)
        return 0;

    int lh = height(root->left);
    int rh = height(root->right);

    return (lh > rh ? lh : rh) + 1;
}

9. Function to Find Diameter

int diameter(struct Node* root) {
    if (root == NULL)
        return 0;

    int lh = height(root->left);
    int rh = height(root->right);

    int currentDiameter = lh + rh + 1;

    int leftDiameter = diameter(root->left);
    int rightDiameter = diameter(root->right);

    return (currentDiameter > leftDiameter ?
           (currentDiameter > rightDiameter ? currentDiameter : rightDiameter)
           : (leftDiameter > rightDiameter ? leftDiameter : rightDiameter));
}

10. Line-by-Line Explanation (Key Parts)

Height Function

  • Calculates maximum depth of subtree

  • Used to compute longest path through a node

Diameter Function

  • lh + rh + 1 → diameter through current node

  • Recursively checks left and right subtree diameters

  • Returns the maximum value


11. Complete C Program

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

int height(struct Node* root) {
    if (root == NULL)
        return 0;

    int lh = height(root->left);
    int rh = height(root->right);

    return (lh > rh ? lh : rh) + 1;
}

int diameter(struct Node* root) {
    if (root == NULL)
        return 0;

    int lh = height(root->left);
    int rh = height(root->right);

    int currentDiameter = lh + rh + 1;

    int leftDiameter = diameter(root->left);
    int rightDiameter = diameter(root->right);

    if (currentDiameter > leftDiameter && currentDiameter > rightDiameter)
        return currentDiameter;
    else if (leftDiameter > rightDiameter)
        return leftDiameter;
    else
        return rightDiameter;
}

int main() {
    struct Node* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    printf("Diameter of the binary tree: %d", diameter(root));
    return 0;
}

12. Output

Diameter of the binary tree: 4

13. Time and Space Complexity

Time Complexity

  • O(n²)
    Height is recalculated for each node.

Space Complexity

  • O(h) due to recursion stack


14. Important Exam Points

  • Diameter is measured in nodes (sometimes edges—check question)

  • Path may or may not include root

  • Brute force method uses height repeatedly

  • Optimized approach exists using single traversal


15. Common Beginner Mistakes

  • Confusing diameter with height

  • Forgetting +1 for current node

  • Assuming diameter always passes through root

  • Returning height instead of diameter


16. Learning Outcome

After this post, learners can:

  • Understand diameter concept clearly

  • Implement diameter calculation in C

  • Analyze recursive tree properties

  • Answer exam/interview questions confidently

ALSO CHECK 

DS(Data Structure) Python&C Edition

DSA Series Chapter 1 Time & Space Complexity ( C Edition)

DSA Series Chapter 2 Arrays  (C Version)


DSA Series Chapter 2 Arrays  (Python Version)

DSA Series Chapter 3 Strings (C Version)


DSA Series Chapter 3 Strings (Python Version)


DSA Series Chapter 4 Linked Lists (Python Version)


DSA Series Chapter 4 Linked Lists (C Version)


DSA Series Chapter 5 Stacks (Python Version)


DSA Series Chapter 5  Stacks  (C Version)




















DSA Series Chapter 13 - Binary Tree traversal, BFS, DFT/DFS, Preorder Traversal In Binary Tree


DSA Series Chapter 14 - InOrder Traversal (Binary Tree C Version)


DSA Series Chapter 14 - InOrder Traversal (Binary Tree Python Version)

 

DSA Series Chapter 15 – POSTORDER TRAVERSAL (BINARY TREE)(Python Version)


DSA Series Chapter 15 – POSTORDER TRAVERSAL (BINARY TREE)(C Version)


DSA Series Chapter 16 – LEVEL ORDER TRAVERSAL(BINARY TREE BFS)(C Version)


DSA Series Chapter 16 – LEVEL ORDER TRAVERSAL(BINARY TREE BFS)(Python Version)



DSA Series Chapter 19 : DELETION IN BINARY TREE(C Version)

DSA Series Chapter 19 : DELETION IN BINARY TREE(Python Version)


DSA Series Chapter 20 – HEIGHT OF BINARY TREE(C Version)

DSA Series Chapter 20 – HEIGHT OF BINARY TREE(Python Version)






Tail Recursion Optimization: How Compilers Convert Recursion Into Iteration


Advanced Recursion: Memoization vs. Tabulation — The Deep Optimization Blueprint for Professionals


Advanced Sorting Algorithms: Merge Sort Internals — Merge Tree, Cache Behavior & Real Cost Analysis


Enjoyed this post? [Follow here] for daily coding insights

Comments