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)
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- 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
- Linked complete binary tree & its creation
- Inorder Successor of a node in Binary Tree
- Construct a Binary Tree from Postorder and Inorder
- Find the largest Complete Subtree in a given Binary Tree
- Iterative Boundary traversal of Complete Binary tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Construct Special Binary Tree from given Inorder 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.