Given a complete binary tree, the task is to find the sum of mirror image nodes in an inorder way i.e. find the inorder traversal of the left sub-tree and for every node traversed, add the value of its mirror node to the current node’s value.
Inorder traversal of the left sub-tree of the given tree is 4 23 11 5.
Adding the mirror nodes,
4 + 16 = 20
23 + 28 = 51
11 + 8 = 19
5 + 5 = 10
Approach: We will use 2 pointers to maintain 2 nodes which are the mirror image of each other. So let’s take root1 and root2 are 2 mirror image nodes. Now left child of root1 and right child of root2 will be the mirror image of each other. We will pass these two nodes (root1->left and root2->right) for next recursive call. Since we have to traverse in an inorder manner so once left sub-tree is traversed then we print the current root data and then we traverse the right sub-tree. Similarly for the right sub-tree so right child of root1 and left child of root2 will be the mirror image of each other. We will pass these two nodes (root1->right and root2->left) for next recursive call.
Below is the implementation of the above approach
20 51 19 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of edges in mirror image of Complete binary tree
- Height of a complete binary tree (or Heap) with N nodes
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Print path from root to all nodes in a Complete Binary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Symmetric Tree (Mirror Image of itself)
- Convert a Binary Tree into its Mirror Tree
- Create a mirror tree from the given binary tree
- Find mirror of a given node in Binary tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Construct a Binary Tree from Postorder and Inorder
- Inorder Successor of a node in Binary Tree
- Check if Inorder traversal of a Binary Tree is palindrome or not
- Print K inorder successors of a Binary Tree in O(1) space
- Construct Special Binary Tree from given Inorder traversal
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Check if an array represents Inorder of Binary Search tree or not
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Linked complete binary tree & its creation
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.