Inverting a binary tree in JavaScript

Inverting a binary tree means creating a mirror image where all left and right child nodes are swapped recursively. This is a classic tree manipulation problem that demonstrates recursion and tree traversal concepts.

What is a Binary Tree?

A binary tree is a hierarchical data structure where each node has at most two children: left and right. The topmost node is called the root, and nodes with no children are called leaves.

4 2 7 1 3 Original Tree

What is Tree Inversion?

Tree inversion swaps the left and right children of every node recursively. The result is a mirror image of the original tree.

Creating a Tree Node

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

Tree Traversal Function

We'll use level-order traversal (BFS) to display the tree structure:

function printTree(root) {
    if (!root) return [];
    
    let result = [];
    let queue = [root];
    
    while (queue.length > 0) {
        let currentNode = queue.shift();
        result.push(currentNode.value);
        
        if (currentNode.left) queue.push(currentNode.left);
        if (currentNode.right) queue.push(currentNode.right);
    }
    
    return result;
}

Binary Tree Inversion Algorithm

The inversion algorithm uses recursion to swap left and right children at each node:

function invertBinaryTree(root) {
    // Base case: empty tree
    if (root === null) return null;
    
    // Recursively invert left and right subtrees
    invertBinaryTree(root.left);
    invertBinaryTree(root.right);
    
    // Swap left and right children
    let temp = root.left;
    root.left = root.right;
    root.right = temp;
    
    return root;
}

Complete Example

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

function printTree(root) {
    if (!root) return [];
    
    let result = [];
    let queue = [root];
    
    while (queue.length > 0) {
        let currentNode = queue.shift();
        result.push(currentNode.value);
        
        if (currentNode.left) queue.push(currentNode.left);
        if (currentNode.right) queue.push(currentNode.right);
    }
    
    return result;
}

function invertBinaryTree(root) {
    if (root === null) return null;
    
    // Recursively invert subtrees
    invertBinaryTree(root.left);
    invertBinaryTree(root.right);
    
    // Swap left and right children
    let temp = root.left;
    root.left = root.right;
    root.right = temp;
    
    return root;
}

// Create original tree
let originalTree = new Node(4);
originalTree.left = new Node(2);
originalTree.right = new Node(7);
originalTree.left.left = new Node(1);
originalTree.left.right = new Node(3);
originalTree.right.left = new Node(9);
originalTree.right.right = new Node(0);

console.log("Original tree:", printTree(originalTree));

// Invert the tree
invertBinaryTree(originalTree);

console.log("Inverted tree:", printTree(originalTree));
Original tree: [ 4, 2, 7, 1, 3, 9, 0 ]
Inverted tree: [ 4, 7, 2, 0, 9, 3, 1 ]

Visual Representation

4 2 7 1 3 9 0 Original Invert 4 7 2 0 9 3 1 Inverted

Time and Space Complexity

Aspect Complexity Explanation
Time O(n) Visit each node exactly once
Space O(h) Recursion stack depth equals tree height

Where n is the number of nodes and h is the height of the tree. In the worst case (skewed tree), h = n, making space complexity O(n).

Conclusion

Binary tree inversion is a fundamental recursive algorithm that swaps left and right children at each node. The solution demonstrates post-order traversal and achieves O(n) time complexity with O(h) space complexity for the recursion stack.

Updated on: 2026-03-15T23:19:00+05:30

888 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements