Mirror of a Binary tree in Kotlin

A mirror of a binary tree is a tree that is obtained by swapping the left and right subtrees of each node in the original tree. In other words, the mirror image of a binary tree is the reflection of the tree along its vertical axis.

For example, consider the following binary tree:

             1
         /      \
       2         3
    /    \     /    \
  4      5  6       7

The mirror image of this tree is:

              1
           /      \
         3         2
      /    \     /    \
     7     6   5     4

To obtain the mirror image of a binary tree, we can perform a recursive traversal of the tree and swap the left and right subtrees of each node. Here is some sample Kotlin code to perform this operation:

Kotlin
class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

fun mirror(root: TreeNode?) {
    if (root == null) {
        return
    }
    
    mirror(root.left)
    mirror(root.right)
    
    val temp = root.left
    root.left = root.right
    root.right = temp
}

In this code, the mirror function takes a binary tree represented by its root node as input and recursively swaps the left and right subtrees of each node. The function first checks if the root node is null (i.e., if the current subtree is empty), and if so, it simply returns. If the root node is not null, the function recursively swaps the left and right subtrees of its left and right children, respectively. Finally, the function swaps the left and right subtrees of the root node itself, using a temporary variable to store one of the subtrees during the swap.

Kotlin
class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

fun mirror(root: TreeNode?) {
    if (root == null) {
        return
    }
    
    mirror(root.left)
    mirror(root.right)
    
    val temp = root.left
    root.left = root.right
    root.right = temp
}

fun main() {
    // create the original binary tree
    val root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left?.left = TreeNode(4)
    root.left?.right = TreeNode(5)
    root.right?.left = TreeNode(6)
    root.right?.right = TreeNode(7)
    
    // print the inorder traversal of the original tree
    println("Inorder traversal of original tree:")
    inorder(root)
    println()
    
    // mirror the binary tree
    mirror(root)
    
    // print the inorder traversal of the mirrored tree
    println("Inorder traversal of mirrored tree:")
    inorder(root)
    println()
}

fun inorder(root: TreeNode?) {
    if (root == null) {
        return
    }
    
    inorder(root.left)
    print("${root.`val`} ")
    inorder(root.right)
}

In this code, the main function creates a binary tree with the same structure as the example I used in my previous answer. It then calls the inorder function to print the inorder traversal of the original tree. The mirror function is then called to create the mirror image of the original tree. Finally, the inorder function is called again to print the inorder traversal of the mirrored tree.

Leave a Comment