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.
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.
Mirror of node 4 is node 6
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Create a mirror tree from the given binary tree
- Convert a Binary Tree into its Mirror Tree
- Find the Deepest Node in a Binary Tree
- Find the parent of a node in the given binary tree
- Find the maximum node at a given level in a binary tree
- Find n-th node in Postorder traversal of a Binary Tree
- Find distance from root to given node in a binary tree
- Find the node with maximum value in a Binary Search Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Find the color of given node in an infinite binary tree
- Find the node with minimum value in a Binary Search Tree
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Find the node with minimum value in a Binary Search Tree using recursion
- Find parent of given node in a Binary Tree with given postorder traversal
- Find the kth node in vertical order traversal of a Binary Tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Number of edges in mirror image of Complete binary tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Find farthest node from each node in Tree