Tree Isomorphism Problem

Pattern: Depth, diameter and balance

medium

Two trees are isomorphic when one can be turned into the other by swapping the children of some nodes. Recursively: the roots must match, and the children must match either as they are or swapped.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Return whether tree b can be obtained from tree a by swapping the left and right children of any number of nodes. The catalogue has no practice link for this one. Trees in the examples are written in level order, with null for a missing child, as on LeetCode.

Think first

  • What may change between the two trees?
  • For one node, what are the two possibilities?
  • What must always match?

Hints

    Brute force, and why it fails

    Try every set of swaps: exponential.

    The key insight

    At each node there are only two ways to pair the children, so recursing on both pairings decides it.

    Dry run

    a = [1, 2, 3, 4, 5, 6, null, null, null, 7, 8], b = [1, 3, 2, null, 6, 4, 5, null, null, null, null, 8, 7]. Roots 1 and 1.

    Step 1 of 3

    The plan

    iso(a, b): if a or b is null: return a == b
               if a.val != b.val: return false
               return (iso(a.l, b.l) and iso(a.r, b.r)) or (iso(a.l, b.r) and iso(a.r, b.l))
    

    Complexity

    Time
    O(n²) worst case
    Space
    O(h)

    Trying both pairings can repeat work; canonical forms (sort each node's two child keys) make it O(n log n).

    Edge cases

    • Both empty: true.
    • Different values: false.
    • Different sizes: false.
    I've built my logic, show the code

    Java

    class Solution {
        public boolean isIsomorphic(TreeNode a, TreeNode b) {
            if (a == null || b == null) return a == b;
            if (a.val != b.val) return false;
            return (isIsomorphic(a.left, b.left) && isIsomorphic(a.right, b.right))     // children as they are
                || (isIsomorphic(a.left, b.right) && isIsomorphic(a.right, b.left));    // or swapped
        }
    }

    C++

    class Solution {
    public:
        bool isIsomorphic(TreeNode* a, TreeNode* b) {
            if (a == NULL || b == NULL) return a == b;
            if (a->val != b->val) return false;
            return (isIsomorphic(a->left, b->left) && isIsomorphic(a->right, b->right))   // children as they are
                || (isIsomorphic(a->left, b->right) && isIsomorphic(a->right, b->left));  // or swapped
        }
    };

    Python

    class Solution:
        def isIsomorphic(self, a, b) -> bool:
            if not a or not b:
                return a is b
            if a.val != b.val:
                return False
            return ((self.isIsomorphic(a.left, b.left) and self.isIsomorphic(a.right, b.right))     # children as they are
                    or (self.isIsomorphic(a.left, b.right) and self.isIsomorphic(a.right, b.left)))  # or swapped

    Common mistakes

    • Trying only the swapped pairing.
    • Allowing value changes.

    Similar problems