Inverting a Binary Tree with Recursion: A Step-by-Step Explanation

·

3 min read

Have you ever come across a coding problem that seems simple at first glance, but then leaves you scratching your head when you dive into the recursive solution? Well, that's exactly how I felt when I was trying to invert a binary tree using recursion.

What does it mean to invert a binary tree? Essentially, it involves swapping the left and right subtrees of each node in the tree. For example, if we have the following binary tree:

    1
   / \
  2   3
 / \   \
4   5   6

After inversion, the tree should look like this:

    1
   / \
  3   2
   \  / \
   6 5   4

Seems simple enough, right? Well, let's dive into the recursive solution and break it down step-by-step.

First, we need to understand the base case. If the root of the tree is None (empty tree), there's nothing to invert, so we simply return None.

Now, for the recursive case, we follow these steps:

  1. Recursively invert the left subtree.

  2. Recursively invert the right subtree.

  3. Swap the left and right subtrees of the current node.

Here's what the code looks like in Python:

def invertTree(root):
    # Base case: If the root is None, return None
    if not root:
        return None

    # Recursive case:
    # 1. Invert the left subtree
    root.left = invertTree(root.left)

    # 2. Invert the right subtree
    root.right = invertTree(root.right)

    # 3. Swap the left and right subtrees
    root.left, root.right = root.right, root.left

    return root

Let's go through an example to see how this recursive function works:

    1
   / \
  2   3
 / \   \
4   5   6
  1. We start by checking if the root (1) is None. It's not, so we proceed.

  2. We recursively call invertTree(root.left), which means we're now working on the left subtree with root 2.

  3. Inside this recursive call, we repeat steps 1-3 for the subtree rooted at 2.

    • We recursively invert the left subtree (4) and the right subtree (5).

    • Since 4 and 5 are leaf nodes, the recursive calls return them as is.

    • We then swap the left and right children of 2, resulting in:

           2
          / \
         5   4
      
  4. We do the same for the right subtree rooted at 3.

    • We recursively invert the right subtree (6), which is a leaf node, so it's returned as is.

    • Since 3 has no left child, we skip that recursive call.

    • We swap the left and right children of 3, which effectively does nothing since the left child is None.

  5. Finally, we swap the left and right children of the root node 1, resulting in:

        1
       / \
      3   2
       \  / \
       6 5   4
    

And there you have it! The binary tree has been successfully inverted using recursion.

The key to understanding this recursive solution is to realize that we're breaking down the problem into smaller subproblems (inverting the left and right subtrees), solving those subproblems recursively, and then combining the solutions by swapping the left and right children of the current node.

I hope this step-by-step explanation has helped demystify the recursive solution to inverting a binary tree. Remember, recursion can be tricky at first, but with practice and a good understanding of the base case and recursive case, you'll be able to tackle these problems like a pro!