Example 5 : Check two binary tree are identical or not

TopicDifficultyCompanies
Binary Tree
EASY
Amazon

Given two binary trees, write a program to check if they are identical or not.

Problem Note

  • Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
  • If the binary trees are same, the output is 1, else 0.

Example 1

Input:     3         3
/ \ / \
4 5 4 5

[3,4,5] [3,4,5]

Output: 1
Explanation: The above binary trees are structurally identical and have the same values.

Example 2

Input:     2         2
/ \
4 4

[2,4] [2,null,4]
Output: 0
Explanation: The above binary trees have the same values but are not structurally identical.

Code Editor

Practice and Learn

Best way to learn is through solving real problems. Practice this problem in this code editor.