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
- Number of edges in mirror image of Complete binary tree
- Height of a complete binary tree (or Heap) with N nodes
- Print path from root to all nodes in a Complete Binary Tree
- Symmetric Tree (Mirror Image of itself)
- Convert a Binary Tree into its Mirror Tree
- Create a mirror tree from the given binary tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Find mirror of a given node in Binary tree
- Construct a Binary Tree from Postorder and Inorder
- Inorder Successor of a node in Binary Tree
- Construct Special Binary Tree from given Inorder traversal
- Check if an array represents Inorder of Binary Search tree or not
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Calculate height of Binary Tree using Inorder and Level Order Traversal
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.