Check if Two Trees are Identitical

Tree are Identical or Not

Given root nodes of 2 trees and we need to find that whether the trees are identical or not.
Trees being identical means that they are same in terms of structures, same in terms of the number of nodes and also same in terms of links.

check if trees are identical or not

Check if Two Trees are Identical

The Algorithm for problem is –

  1. Check whether the data of root nodes of both are same or not.
  2. Do this recursively for left subtree and right subtree.
  3. Check for corner cases, like when one of the node is absent in the trees ans so on.
Check whether two binary trees are identical or not in java

Java Program to Check if Two Trees are Identical or Not

Run
class Node
{
  int data;
  Node left, right;

    Node (int item)
  {
    data = item;
    left = right = null;
  }
}
class BTree
{
  Node root1, root2, root3;
  boolean isIdentical (Node root1, Node root2)
  {
    //Null Node 
    if (root1 == null && root2 == null)
      {
	return true;
      }
    // Cases where One node is null and the other is not.
    if (root1 == null && root2 != null)
      return false;
    if (root1 != null && root2 == null)
      return false;
    if (root1.data != root2.data)
      return false;

    //Do this recursively for Left and right tree.
    return (isIdentical (root1.left, root2.left)
	    && isIdentical (root1.right, root2.right));
  }
}

public class Main
{
  public static void main (String[]args)
  {
    BTree tree = new BTree ();

      tree.root1 = new Node (1);
      tree.root1.left = new Node (2);
      tree.root1.right = new Node (3);
      tree.root1.left.left = new Node (4);
      tree.root1.left.right = new Node (5);

      tree.root2 = new Node (1);
      tree.root2.left = new Node (2);
      tree.root2.right = new Node (3);
      tree.root2.left.left = new Node (4);
      tree.root2.left.right = new Node (5);

      tree.root3 = new Node (1);
      tree.root3.left = new Node (2);
      tree.root3.right = new Node (3);
      tree.root3.left.left = new Node (10);
      tree.root3.left.right = new Node (11);

      System.out.println ("Tree 1 and Tree 2 : ");
    if (tree.isIdentical (tree.root1, tree.root2))
        System.out.println ("Both trees are identical");
    else
        System.out.println ("Trees are not identical");

      System.out.println ("\nTree 2 and Tree 3 : ");
    if (tree.isIdentical (tree.root3, tree.root2))
        System.out.println ("Both trees are identical");
    else
        System.out.println ("Trees are not identical");

  }
}

Output:

Tree 1 and Tree 2 : 
Both trees are identical

Tree 2 and Tree 3 : 
Trees are not identical

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java