HomeLeetcode226. Invert Binary Tree - Leetcode Solutions

226. Invert Binary Tree – Leetcode Solutions

Description

Given the root of a binary tree, invert the tree, and return its root.

Examples:

Example 1:

Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]

Example 2:

Input: root = [2,1,3]
Output: [2,3,1]

Example 3:

Input: root = []
Output: []

Solution in Python

Approach:

  1. Base Case:
    • If the root is None (empty tree), we simply return None.
  2. Recursive Inversion:
    • For each node, we recursively invert its left and right subtrees.
    • Once the subtrees are inverted, we swap the left and right children of the current node.
  3. Return the Inverted Tree:
    • After swapping the left and right subtrees, return the root, which now represents the root of the inverted tree.

Steps:

  1. Check if the root is None. If so, return None.
  2. Recursively invert the left subtree.
  3. Recursively invert the right subtree.
  4. Swap the left and right subtrees of the current node.
  5. Return the current node, which now represents the root of the inverted subtree.
Python
class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        # Base case: if the root is None, return None
        if root is None:
            return None
        
        # Recursive case: invert the left and right subtrees
        left_inverted = self.invertTree(root.left)  # Invert the left subtree
        right_inverted = self.invertTree(root.right)  # Invert the right subtree
        
        # Swap the left and right subtrees
        root.left = right_inverted
        root.right = left_inverted
        
        # Return the root, which now represents the inverted tree
        return root

Explanation:

  1. Base Case:
    • If the root is None, the function immediately returns None. This handles the case where the tree is empty or we have reached a leaf node’s child.
  2. Recursive Case:
    • We first invert the left subtree and store the result in left_inverted.
    • Similarly, we invert the right subtree and store the result in right_inverted.
  3. Swapping:
    • Once we have the inverted left and right subtrees, we swap them by assigning root.left to right_inverted and root.right to left_inverted.
  4. Return:
    • Finally, the root (with its children swapped) is returned.

Time Complexity:

  • O(n): where n is the number of nodes in the tree. Each node is visited exactly once, and the inversion operation at each node takes constant time.

Space Complexity:

  • O(h): where h is the height of the tree. This accounts for the recursion stack. In the worst case, the height of the tree could be n (in a skewed tree), making the space complexity O(n). In the best case (balanced tree), the height is O(log n).

Solution in Javascript

JavaScript
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    // Base case: if the root is null, return null (empty tree or leaf node's child)
    if (root === null) {
        return null;
    }

    // Recursively invert the left and right subtrees
    let leftInverted = invertTree(root.left);   // Invert the left subtree
    let rightInverted = invertTree(root.right); // Invert the right subtree

    // Swap the left and right children of the current node
    root.left = rightInverted;
    root.right = leftInverted;

    // Return the root, which now represents the inverted tree
    return root;
};

Solution in Java

Java
class Solution {
    public TreeNode invertTree(TreeNode root) {
        // Base case: if the root is null, return null (empty tree or leaf node's child)
        if (root == null) {
            return null;
        }

        // Recursively invert the left and right subtrees
        TreeNode leftInverted = invertTree(root.left);   // Invert the left subtree
        TreeNode rightInverted = invertTree(root.right); // Invert the right subtree

        // Swap the left and right children of the current node
        root.left = rightInverted;
        root.right = leftInverted;

        // Return the root, which now represents the inverted tree
        return root;
    }
}

Solution in C#

C#
public class Solution {
    public TreeNode InvertTree(TreeNode root) {
        // Base case: If the root is null, return null (empty tree or leaf node's child)
        if (root == null) {
            return null;
        }

        // Recursively invert the left and right subtrees
        TreeNode leftInverted = InvertTree(root.left);   // Invert the left subtree
        TreeNode rightInverted = InvertTree(root.right); // Invert the right subtree

        // Swap the left and right children of the current node
        root.left = rightInverted;
        root.right = leftInverted;

        // Return the root, which now represents the inverted tree
        return root;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular