How to invert a binary search tree using recursion in C#?

To invert a binary search tree, we swap the left and right child nodes for every node in the tree recursively. This operation creates a mirror image of the original tree. The algorithm uses a recursive approach where we process each node by first inverting its left and right subtrees, then swapping their positions.

The process involves calling a method InvertBinaryTree that takes a node as a parameter. If the node is null, we return null. Otherwise, we recursively call the method on both left and right children, then swap the left and right child references.

Binary Tree Inversion Original Tree Inverted Tree 1 3 2 1 2 3 swap Left and right children are swapped at each node

Syntax

Following is the basic recursive structure for inverting a binary tree −

public Node InvertBinaryTree(Node node) {
    if (node == null) {
        return null;
    }
    Node left = InvertBinaryTree(node.LeftChild);
    Node right = InvertBinaryTree(node.RightChild);
    node.LeftChild = right;
    node.RightChild = left;
    return node;
}

How It Works

The inversion algorithm follows these steps −

  • Base Case: If the current node is null, return null.

  • Recursive Calls: Recursively invert the left and right subtrees.

  • Swap: Assign the inverted right subtree to the left child and the inverted left subtree to the right child.

  • Return: Return the current node with its children swapped.

Example

using System;

public class TreesPgm {
    public class Node {
        public int Value;
        public Node LeftChild;
        public Node RightChild;
        
        public Node(int value) {
            this.Value = value;
        }
        
        public override String ToString() {
            return "Node=" + Value;
        }
    }
    
    public Node InvertBinaryTree(Node node) {
        if (node == null) {
            return null;
        }
        Node left = InvertBinaryTree(node.LeftChild);
        Node right = InvertBinaryTree(node.RightChild);
        node.LeftChild = right;
        node.RightChild = left;
        return node;
    }
    
    public void InOrderTraversal(Node node) {
        if (node != null) {
            InOrderTraversal(node.LeftChild);
            Console.Write(node.Value + " ");
            InOrderTraversal(node.RightChild);
        }
    }
    
    public static void Main(string[] args) {
        TreesPgm tree = new TreesPgm();
        Node root = new Node(1);
        root.LeftChild = new Node(3);
        root.RightChild = new Node(2);
        
        Console.WriteLine("Original tree (In-order):");
        tree.InOrderTraversal(root);
        Console.WriteLine();
        
        root = tree.InvertBinaryTree(root);
        
        Console.WriteLine("Inverted tree (In-order):");
        tree.InOrderTraversal(root);
    }
}

The output of the above code is −

Original tree (In-order):
3 1 2 
Inverted tree (In-order):
2 1 3 

Iterative Approach Using Stack

Example

using System;
using System.Collections.Generic;

public class TreeProgram {
    public class Node {
        public int Value;
        public Node LeftChild;
        public Node RightChild;
        
        public Node(int value) {
            this.Value = value;
        }
    }
    
    public Node InvertBinaryTreeIterative(Node root) {
        if (root == null) return null;
        
        Stack<Node> stack = new Stack<Node>();
        stack.Push(root);
        
        while (stack.Count > 0) {
            Node node = stack.Pop();
            
            // Swap left and right children
            Node temp = node.LeftChild;
            node.LeftChild = node.RightChild;
            node.RightChild = temp;
            
            // Add children to stack for processing
            if (node.LeftChild != null) stack.Push(node.LeftChild);
            if (node.RightChild != null) stack.Push(node.RightChild);
        }
        
        return root;
    }
    
    public void PreOrderTraversal(Node node) {
        if (node != null) {
            Console.Write(node.Value + " ");
            PreOrderTraversal(node.LeftChild);
            PreOrderTraversal(node.RightChild);
        }
    }
    
    public static void Main(string[] args) {
        TreeProgram tree = new TreeProgram();
        Node root = new Node(4);
        root.LeftChild = new Node(2);
        root.RightChild = new Node(7);
        root.LeftChild.LeftChild = new Node(1);
        root.LeftChild.RightChild = new Node(3);
        
        Console.WriteLine("Original tree (Pre-order):");
        tree.PreOrderTraversal(root);
        Console.WriteLine();
        
        root = tree.InvertBinaryTreeIterative(root);
        
        Console.WriteLine("Inverted tree (Pre-order):");
        tree.PreOrderTraversal(root);
    }
}

The output of the above code is −

Original tree (Pre-order):
4 2 1 3 7 
Inverted tree (Pre-order):
4 7 3 1 2 

Time and Space Complexity

Approach Time Complexity Space Complexity
Recursive O(n) O(h) where h is tree height
Iterative O(n) O(n) for the stack

Conclusion

Inverting a binary tree involves swapping left and right children for every node in the tree. Both recursive and iterative approaches have O(n) time complexity, where n is the number of nodes. The recursive solution is more intuitive, while the iterative approach using a stack provides an alternative that avoids function call overhead.

Updated on: 2026-03-17T07:04:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements