Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Isomorphic Trees (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given root of two binary trees root1 and root2, return true if they are isomorphic. Otherwise, return false.

Two trees are said to be isomorphic if one tree can be transformed into the other by swapping the left and right children of some nodes.

Note that the structure should be identical after possible swaps, and the values of nodes must be the same in both trees.

Examples

Example 1

  • Input: root1 = [1, 2, 3, 4], root2 = [1, 3, 2, null, null, 4]
Image
  • Output: true
  • Explanation: In the first tree, we can swap 2 with 3, and 4 with null to get the second tree by swapping.

Example 2

  • Input: root1 = [1, 2, 3], root2 = [1, 3, 2]
Image
  • Output: true
  • Explanation: We can swap the left and right children of the root node in Tree 1. This transforms Tree 1 into Tree 2.

Example 3

  • Input: root1 = [1, 2, 3, 4, 5, 6, 7], root2 = [1, 4, 3, 2, 5, 7, 6]
Image
  • Output: false
  • Explanation: We can't get tree2 by performing swap operations in tree1.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible