Find mirror of a given node in Binary tree - GeeksforGeeks

29 Jun, 2021
Given a Binary tree, the problem is to find the mirror of a given node. The mirror of a node is a node which exists at the mirror position of the node in opposite subtree at the root.
Examples:  
In above tree-
Node 2 and 3 are mirror nodes
Node 4 and 6 are mirror nodes. 
We can have a recursive solution for finding mirror nodes. The algorithm is following –  
1) Start from the root of the tree and recur 
   nodes from both subtree simultaneously 
   using two pointers for left and right nodes.
2) First recur all the external nodes and 
   store returned value in mirror variable.
3) If current node value is equal to target node, 
   return the value of opposite pointer else 
   repeat step 2.
4) If no external node is left and mirror is 
   none, recur internal nodes.
C++
C
Java
Python3
C#
Javascript
// C++ program to find the mirror Node// in Binary tree#include <bits/stdc++.h> using namespace std; /* A binary tree Node has data,pointer to left child anda pointer to right child */struct Node{    int key;    struct Node* left, *right;}; // create new Node and initialize itstruct Node* newNode(int key){    struct Node* n = (struct Node*)                      malloc(sizeof(struct Node*));    if (n != NULL)    {        n->key = key;        n->left = NULL;        n->right = NULL;        return n;    }    else    {        cout << "Memory allocation failed!"             << endl;        exit(1);    }} // recursive function to find mirror of Nodeint findMirrorRec(int target, struct Node* left,                              struct Node* right){    /* if any of the Node is none then Node itself    and decendent have no mirror, so return    none, no need to further explore! */    if (left == NULL || right == NULL)        return 0;     /* if left Node is target Node, then return    right's key (that is mirror) and vice    versa */    if (left->key == target)        return right->key;     if (right->key == target)        return left->key;     // first recur external Nodes    int mirror_val = findMirrorRec(target,                                   left->left,                                   right->right);    if (mirror_val)        return mirror_val;     // if no mirror found, recur internal Nodes    findMirrorRec(target, left->right, right->left);} // interface for mirror searchint findMirror(struct Node* root, int target){    if (root == NULL)        return 0;    if (root->key == target)        return target;    return findMirrorRec(target, root->left,                                 root->right);} // Driver Codeint main(){    struct Node* root = newNode(1);    root-> left = newNode(2);    root->left->left = newNode(4);    root->left->left->right    = newNode(7);    root->right    = newNode(3);    root->right->left = newNode(5);    root->right->right = newNode(6);    root->right->left->left    = newNode(8);    root->right->left->right = newNode(9);     // target Node whose mirror have to be searched    int target = root->left->left->key;     int mirror = findMirror(root, target);     if (mirror)        cout << "Mirror of Node " << target             << " is Node " << mirror << endl;    else        cout << "Mirror of Node " << target             << " is NULL! " << endl;} // This code is contributed by SHUBHAMSINGH10
// C program to find the mirror Node in Binary tree#include <stdio.h>#include <stdlib.h> /* A binary tree Node has data, pointer to left child  and a pointer to right child */struct Node{    int key;    struct Node* left, *right;}; // create new Node and initialize itstruct Node* newNode(int key){    struct Node* n = (struct Node*)                     malloc(sizeof(struct Node*));    if (n != NULL)    {        n->key = key;        n->left = NULL;        n->right = NULL;        return n;    }    else    {        printf("Memory allocation failed!");        exit(1);    }} // recursive function to find mirror of Nodeint findMirrorRec(int target, struct Node* left,                              struct Node* right){    /* if any of the Node is none then Node itself       and decendent have no mirror, so return       none, no need to further explore! */    if (left==NULL || right==NULL)        return 0;     /* if left Node is target Node, then return       right's key (that is mirror) and vice       versa */    if (left->key == target)        return right->key;     if (right->key == target)        return left->key;     // first recur external Nodes    int mirror_val = findMirrorRec(target,                                     left->left,                                     right->right);    if (mirror_val)        return mirror_val;     // if no mirror found, recur internal Nodes    findMirrorRec(target, left->right, right->left);} // interface for mirror searchint findMirror(struct Node* root, int target){    if (root == NULL)        return 0;    if (root->key == target)        return target;    return findMirrorRec(target, root->left, root->right);} // Driverint main(){    struct Node* root           = newNode(1);    root-> left                 = newNode(2);    root->left->left            = newNode(4);    root->left->left->right     = newNode(7);    root->right                 = newNode(3);    root->right->left           = newNode(5);    root->right->right          = newNode(6);    root->right->left->left     = newNode(8);    root->right->left->right    = newNode(9);     // target Node whose mirror have to be searched    int target = root->left->left->key;     int mirror = findMirror(root, target);     if (mirror)        printf("Mirror of Node %d is Node %d\n",                                    target, mirror);    else        printf("Mirror of Node %d is NULL!\n", target);}
// Java program to find the mirror Node in Binary treeclass GfG { /* A binary tree Node has data, pointer to left childand a pointer to right child */static class Node{    int key;    Node left, right;} // create new Node and initialize itstatic Node newNode(int key){    Node n = new Node();              n.key = key;        n.left = null;        n.right = null;        return n;} // recursive function to find mirror of Nodestatic int findMirrorRec(int target, Node left, Node right){    /* if any of the Node is none then Node itself    and decendent have no mirror, so return    none, no need to further explore! */    if (left==null || right==null)        return 0;     /* if left Node is target Node, then return    right's key (that is mirror) and vice    versa */    if (left.key == target)        return right.key;     if (right.key == target)        return left.key;     // first recur external Nodes    int mirror_val = findMirrorRec(target, left.left, right.right);    if (mirror_val != 0)        return mirror_val;     // if no mirror found, recur internal Nodes    return findMirrorRec(target, left.right, right.left);} // interface for mirror searchstatic int findMirror(Node root, int target){    if (root == null)        return 0;    if (root.key == target)        return target;    return findMirrorRec(target, root.left, root.right);} // Driverpublic static void main(String[] args){    Node root         = newNode(1);    root.left                 = newNode(2);    root.left.left         = newNode(4);    root.left.left.right     = newNode(7);    root.right                 = newNode(3);    root.right.left         = newNode(5);    root.right.right         = newNode(6);    root.right.left.left     = newNode(8);    root.right.left.right = newNode(9);     // target Node whose mirror have to be searched    int target = root.left.left.key;     int mirror = findMirror(root, target);     if (mirror != 0)        System.out.println("Mirror of Node " + target + " is Node " + mirror);    else        System.out.println("Mirror of Node " + target + " is null ");}}
# Python3 program to find the mirror node in# Binary tree class Node:    '''A binary tree node has data, reference to left child         and a reference to right child '''     def __init__(self, key, lchild=None, rchild=None):        self.key = key        self.lchild = None        self.rchild = None  # recursive function to find mirrordef findMirrorRec(target, left, right):     # If any of the node is none then node itself    # and decendent have no mirror, so return    # none, no need to further explore!    if left == None or right == None:        return None     # if left node is target node, then return    # right's key (that is mirror) and vice versa    if left.key == target:        return right.key    if right.key == target:        return left.key     # first recur external nodes    mirror_val = findMirrorRec(target, left.lchild, right.rchild)    if mirror_val != None:        return mirror_val     # if no mirror found, recur internal nodes    findMirrorRec(target, left.rchild, right.lchild) # interface for mirror searchdef findMirror(root, target):    if root == None:        return None     if root.key == target:        return target     return findMirrorRec(target, root.lchild, root.rchild) # Driverdef main():    root = Node(1)    n1 = Node(2)    n2 = Node(3)    root.lchild = n1    root.rchild = n2    n3 = Node(4)    n4 = Node(5)    n5 = Node(6)    n1.lchild = n3    n2.lchild = n4    n2.rchild = n5    n6 = Node(7)    n7 = Node(8)    n8 = Node(9)    n3.rchild = n6    n4.lchild = n7    n4.rchild = n8     # target node whose mirror have to be searched    target = n3.key     mirror = findMirror(root, target)    print("Mirror of node {} is node {}".format(target, mirror)) if __name__ == '__main__':    main()
// C# program to find the// mirror Node in Binary treeusing System; class GfG{     /* A binary tree Node has data,        pointer to left child and a        pointer to right child */    class Node    {        public int key;        public Node left, right;    }     // create new Node and initialize it    static Node newNode(int key)    {        Node n = new Node();             n.key = key;            n.left = null;            n.right = null;            return n;    }     // recursive function to find mirror of Node    static int findMirrorRec(int target, Node left,                                        Node right)    {        /* if any of the Node is none then Node itself        and decendent have no mirror, so return        none, no need to further explore! */        if (left==null || right==null)            return 0;         /* if left Node is target Node, then return        right's key (that is mirror) and vice        versa */        if (left.key == target)            return right.key;         if (right.key == target)            return left.key;         // first recur external Nodes        int mirror_val = findMirrorRec(target,                        left.left, right.right);        if (mirror_val != 0)            return mirror_val;         // if no mirror found, recur internal Nodes        return findMirrorRec(target,                left.right, right.left);    }     // interface for mirror search    static int findMirror(Node root, int target)    {        if (root == null)            return 0;        if (root.key == target)            return target;        return findMirrorRec(target,                root.left, root.right);    }     // Driver code    public static void Main(String[] args)    {        Node root = newNode(1);        root.left = newNode(2);        root.left.left = newNode(4);        root.left.left.right = newNode(7);        root.right = newNode(3);        root.right.left    = newNode(5);        root.right.right = newNode(6);        root.right.left.left = newNode(8);        root.right.left.right = newNode(9);         // target Node whose mirror have to be searched        int target = root.left.left.key;         int mirror = findMirror(root, target);         if (mirror != 0)            Console.WriteLine("Mirror of Node " +                                target + " is Node " +                                            mirror);        else            Console.WriteLine("Mirror of Node " + target +                                            " is null ");    }} // This code is contributed by 29AjayKumar
<script> // Javascript program to find the mirror// Node in Binary tree /* A binary tree Node has data, pointerto left child and a pointer to right child */class Node{         // Create new Node and initialize it    constructor(key)    {        this.key = key;        this.left = this.right = null;    }} // Recursive function to find mirror of Nodefunction findMirrorRec(target, left, right){         /* If any of the Node is none then Node itself    and decendent have no mirror, so return    none, no need to further explore! */    if (left == null || right == null)        return 0;       /* If left Node is target Node, then return    right's key (that is mirror) and vice    versa */    if (left.key == target)        return right.key;       if (right.key == target)        return left.key;       // First recur external Nodes    let mirror_val = findMirrorRec(        target, left.left, right.right);    if (mirror_val != 0)        return mirror_val;       // If no mirror found, recur internal Nodes    return findMirrorRec(target, left.right,                                 right.left);} // Interface for mirror searchfunction findMirror(root, target){    if (root == null)        return 0;    if (root.key == target)        return target;             return findMirrorRec(target, root.left,                                 root.right);} // Driver codelet root = new Node(1);root.left = new Node(2);root.left.left = new Node(4);root.left.left.right = new Node(7);root.right = new Node(3);root.right.left = new Node(5);root.right.right = new Node(6);root.right.left.left = new Node(8);root.right.left.right = new Node(9); // Target Node whose mirror have to be searchedlet target = root.left.left.key; let mirror = findMirror(root, target); if (mirror != 0)    document.write("Mirror of Node " + target +                   " is Node " + mirror + "<br>");else    document.write("Mirror of Node " + target +                   " is null " + "<br>"); // This code is contributed by rag2127 </script>
Output: 
Mirror of node 4 is node 6
Time Complexity: 
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=lN848OkmMY4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
prerna saini
29AjayKumar
SHUBHAMSINGH10
rag2127
Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Tree Data Structure
DFS traversal of a tree using recursion
Top 50 Tree Coding Problems for Interviews
Find the node with minimum value in a Binary Search Tree
Real-time application of Data Structures
Print Binary Tree in 2-Dimensions
Iterative Postorder Traversal | Set 2 (Using One Stack)
Find maximum (or minimum) in Binary Tree
Difference between Min Heap and Max Heap
Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)