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.
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Number of edges in mirror image of Complete binary tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Convert a Binary Tree into its Mirror Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Create a mirror tree from the given binary tree
- Symmetric Tree (Mirror Image of itself)
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Height of a complete binary tree (or Heap) with N nodes
- Print path from root to all nodes in a Complete Binary Tree
- Find mirror of a given node in Binary tree
- Cartesian tree from inorder traversal | Segment Tree
- Construct Special Binary Tree from given Inorder traversal
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Check if an array represents Inorder of Binary Search tree or not
- Inorder Successor of a node in Binary Tree
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Check if Inorder traversal of a Binary Tree is palindrome or not
- Construct a Binary Tree from Postorder and Inorder
- Print K inorder successors of a Binary Tree in O(1) space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.