DSA SERIES Chapter 26 : CHECK IF TWO BINARY TREES ARE IDENTICAL(C Version)

 

[JOIN HERE] for daily coding insights

DSA SERIES Chapter 26 : CHECK IF TWO BINARY TREES ARE IDENTICAL(C Version)


1. Objective

To determine whether two given binary trees are identical, meaning:

  • They have the same structure

  • They contain the same data at corresponding nodes


2. Definition (Exam-Ready)

Two binary trees are said to be identical if both their structure and node values are exactly the same at every corresponding position.


3. Conditions for Identical Trees

Two trees T1 and T2 are identical if and only if:

  1. Both trees are empty

  2. Root values of both trees are equal

  3. Left subtrees are identical

  4. Right subtrees are identical

If any one condition fails, the trees are not identical.


4. Example

Tree 1

        1
       / \
      2   3

Tree 2

        1
       / \
      2   3

Identical Trees


Non-Identical Case

Tree 1:           Tree 2:
   1                 1
  /                   \
 2                     2

Not Identical (structure differs)


5. Key Observation

  • Tree comparison must be recursive

  • Iterative comparison is complex and not preferred

  • Preorder comparison logic fits naturally


6. Recursive Logic

For nodes n1 and n2:

if both nodes are NULL → identical
if one node is NULL → not identical
if data differs → not identical
else:
    check left subtrees
    check right subtrees

7. Structure Definition (Node)

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

8. Function to Check Identical Trees

int areIdentical(struct Node* root1, struct Node* root2)
{
    if (root1 == NULL && root2 == NULL)
        return 1;

    if (root1 == NULL || root2 == NULL)
        return 0;

    if (root1->data != root2->data)
        return 0;

    return areIdentical(root1->left, root2->left) &&
           areIdentical(root1->right, root2->right);
}

9. Line-by-Line Explanation

if (root1 == NULL && root2 == NULL)
    return 1;
  • Both subtrees are empty

  • Hence identical


if (root1 == NULL || root2 == NULL)
    return 0;
  • One tree has a node while the other does not

  • Structures differ


if (root1->data != root2->data)
    return 0;
  • Node values do not match


return areIdentical(root1->left, root2->left) &&
       areIdentical(root1->right, root2->right);
  • Recursively compare left and right subtrees

  • Logical AND ensures both must be identical


10. 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 areIdentical(struct Node* root1, struct Node* root2)
{
    if (root1 == NULL && root2 == NULL)
        return 1;

    if (root1 == NULL || root2 == NULL)
        return 0;

    if (root1->data != root2->data)
        return 0;

    return areIdentical(root1->left, root2->left) &&
           areIdentical(root1->right, root2->right);
}

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

    struct Node* root2 = createNode(1);
    root2->left = createNode(2);
    root2->right = createNode(3);

    if (areIdentical(root1, root2))
        printf("Both trees are IDENTICAL");
    else
        printf("Trees are NOT identical");

    return 0;
}

11. Output

Both trees are IDENTICAL

12. Time and Space Complexity

Time Complexity

  • O(n)
    (Each node is visited once)

Space Complexity

  • O(h)
    (Recursive call stack, h = height of tree)


13. Important Exam & Interview Points

  • Must check both structure and data

  • Recursive solution is standard

  • Used in:

    • Tree comparison

    • Tree validation problems

    • Subtree checking


14. Common Student Mistakes

  • Comparing only inorder traversal

  • Ignoring structure

  • Missing NULL checks

  • Using OR instead of AND in recursion


15. Learning Outcome

After this post, students can:

  • Understand structural comparison of trees

  • Implement recursive tree comparison

  • Solve exam and interview problems 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)

DSA Series Chapter 21 : COUNT TOTAL NODES IN BINARY TREE(C Version)

DSA Series Chapter 21 : COUNT TOTAL NODES IN 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