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



[Follow here] for daily coding insights 

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


1. Objective

To compute the height of a Binary Tree using a recursive approach in C.


2. What is Height of a Binary Tree?

Definition (Exam-Standard)

The height of a binary tree is the number of nodes on the longest path from the root node to the deepest leaf node.

***** Some books define height in edges.*****
In this post, we follow node-based height, which is most common in Indian university syllabi.


3. Example Binary Tree

            1
          /   \
         2     3
        / \
       4   5

Height Calculation

  • Longest path: 1 → 2 → 4

  • Number of nodes = 3

Hence, Height = 3


4. Key Concept Behind Height Calculation

  • Height depends on maximum depth

  • Recursively compute:

    • Height of left subtree

    • Height of right subtree

  • Take maximum of both

  • Add 1 for current node


5. Formula Used

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

Base Condition

height(NULL) = 0

6. Data Structure Used

Node Structure

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

7. Height Function (Core Logic)

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

    int leftHeight = height(root->left);
    int rightHeight = height(root->right);

    return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}

8. Line-by-Line Explanation

int height(struct Node* root)
  • Function returns an integer (height)

  • root is current node

if (root == NULL)
    return 0;
  • Empty tree has height 0

  • Acts as base case

int leftHeight = height(root->left);
  • Recursively compute left subtree height

int rightHeight = height(root->right);
  • Recursively compute right subtree height

return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
  • Take maximum height

  • Add 1 for current node


9. 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 leftHeight = height(root->left);
    int rightHeight = height(root->right);

    return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}

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("Height of Binary Tree: %d\n", height(root));

    return 0;
}

10. Output

Height of Binary Tree: 3

11. Time and Space Complexity

Time Complexity

  • O(n) — every node visited once

Space Complexity

  • O(h) — recursion stack

  • Worst case (skewed tree): O(n)


12. Important Exam Notes

  • Height is calculated using DFS (recursion)

  • BFS is not required

  • Base condition must return 0

  • Difference between height and depth is important


13. Common Student Mistakes

  • Returning -1 instead of 0

  • Forgetting +1

  • Mixing node-based and edge-based height

  • Using BFS unnecessarily


14. Height vs Depth (Quick Comparison)

TermMeaning
HeightLongest path from root to leaf
DepthDistance from root to a node

15. Learning Outcome

After this post, students can:

  • Define height of a binary tree

  • Implement recursive height calculation

  • Analyze time & space complexity

  • Answer exam 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 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