Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Identical Binary Trees Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Identical Binary Trees

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

If two binary trees share the exact same structure and have the same node values, they are considered identical.

Given references to the root nodes of two binary trees, find out if the trees are identical or not.

Approach (BFS)

The idea is to do a level order traversal for both the trees simultaneously and compare the corresponding elements of both the trees.

Analysis

  • Time Complexity: O(n + m)
  • Auxiliary Space Complexity: O(n + m)

Implementation

C++
/* This is the Node class definition

class Node {
public:
    Node* left;
    Node* right;
    int data;

    Node(int data) {
        this->left = NULL;
        this->right = NULL;
        this->data = data;
    }
};
*/

bool areIdenticalTrees(Node* root1, Node* root2) {
    queue<Node*> tree1, tree2;
	tree1.push(root1);
	tree2.push(root2);
	while (!tree1.empty() && !tree2.empty()) {
		Node* currentNode1 = tree1.front();
		tree1.pop();
		Node* currentNode2 = tree2.front();
		tree2.pop();
		if (currentNode1->data != currentNode2->data) {
			return false;
		}
		if (currentNode1->left != NULL) {
			tree1.push(currentNode1->left);
		}
		if (currentNode1->right != NULL) {
			tree1.push(currentNode1->right);
		}
		if (currentNode2->left != NULL) {
			tree2.push(currentNode2->left);
		}
		if (currentNode2->right != NULL) {
			tree2.push(currentNode2->right);
		}
	}
	if (!tree1.empty() || !tree2.empty()) {
		return false;
	}
	return true;
}
Java
class Solution {
	/* This is the Node class definition
	
	class Node {
		public Node left;
		public Node right;
		public int data;

		public Node(int data) {
			this.data = data;
		}
	}
	*/
	
	boolean areIdenticalTrees(Node root1, Node root2) {
	    Queue<Node> tree1 = new LinkedList<Node> ();
	    Queue<Node> tree2 = new LinkedList<Node> ();
		tree1.add(root1);
		tree2.add(root2);
		while (!tree1.isEmpty() && !tree2.isEmpty()) {
			Node currentNode1 = tree1.peek();
			tree1.poll();
			Node currentNode2 = tree2.peek();
			tree2.poll();
			if (currentNode1.data != currentNode2.data) {
				return false;
			}
			if (currentNode1.left != null) {
				tree1.add(currentNode1.left);
			}
			if (currentNode1.right != null) {
				tree1.add(currentNode1.right);
			}
			if (currentNode2.left != null) {
				tree2.add(currentNode2.left);
			}
			if (currentNode2.right != null) {
				tree2.add(currentNode2.right);
			}
		}
		if (!tree1.isEmpty() || !tree2.isEmpty()) {
			return false;
		}
		return true;
	}
}

Approach (DFS)

The idea is to traverse both the trees recursively and check if every node and its children subtrees for both the trees are equal or not.

Analysis

  • Time Complexity: O(n + m)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
/* This is the Node class definition

class Node {
public:
    Node* left;
    Node* right;
    int data;

    Node(int data) {
        this->left = NULL;
        this->right = NULL;
        this->data = data;
    }
};
*/

bool areIdenticalTrees(Node* root1, Node* root2) {
    if(root1 == NULL && root2 == NULL) {
		return true;
	}
	if(root1 == NULL || root2 == NULL) {
		return false;
	}
	if(root1->data != root2->data) {
		return false;
	}
	if(areIdenticalTrees(root1->left, root2->left) == false || areIdenticalTrees(root1->right, root2->right) == false) {
		return false;
	}
	return true;
}
Java
class Solution {
	/* This is the Node class definition
	
	class Node {
		public Node left;
		public Node right;
		public int data;

		public Node(int data) {
			this.data = data;
		}
	}
	*/
	
	boolean areIdenticalTrees(Node root1, Node root2) {
	    if(root1 == null && root2 == null) {
			return true;
		}
		if(root1 == null || root2 == null) {
			return false;
		}
		if(root1.data != root2.data) {
			return false;
		}
		if(areIdenticalTrees(root1.left, root2.left) == false || areIdenticalTrees(root1.right, root2.right) == false) {
			return false;
		}
		return true;
	}
}
Related Content
Balanced Binary Tree
Binary Search Tree (BST) Iterator
Binary Tree to Doubly Linked List
Binary Tree Inorder Traversal
Maximum Path Sum of Binary Tree
Binary Tree Postorder Traversal
Binary Tree Preorder Traversal
Bottom View of Binary Tree
Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal
Delete Node in a Binary Search Tree (BST)
Diameter of Binary Tree
Flatten Binary Tree to Linked List
Inorder Predecessor of Node in BST
Inorder Successor of Node in BST
Insert into a Binary Search Tree (BST)
Invert Binary Tree
Is Binary Tree BST
Kth Largest in BST
Kth Smallest in BST
Left View of Binary Tree
Level Order of Binary Tree
Lowest Common Ancestor in Binary Tree
Lowest Common Ancestor in BST
Maximum Depth of Binary Tree
Populating Next Right Pointers in Each Node
Right View of Binary Tree
Search in a Binary Search Tree (BST)
Serialize and Deserialize Binary Search Tree (BST)
Size of the Largest BST in a Binary Tree
Symmetric Binary Tree
Top View of Binary Tree
Two Sum in BST
Binary Tree Zigzag Level Order Traversal
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet