
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of the mirror image nodes of a complete binary tree in an inorder way in C++
In this problem, we are given a complete binary tree. Our task is to create a program to find the sum of the mirror image nodes of a complete binary tree in an inorder way.
Here, we have to find the inorder traversal of the left sun-tree, and then for each node, we will add the mirror image with it. This means if we are traversing the left leaf node, we will add the value of the right leaf node with it. As it is the mirror image node.
Some important definitions
Complete binary tree is a binary tree where all levels have the highest number of nodes except the last level.
Inorder traversal is a tree traversal technique in which the left subtree is visited first, the root is visited and the right sub-tree is visited.
Let’s take an example to understand the problem,
Input
Output − 9 9 17 2
Explanation − Inorder traversal of the left subtree is 5-7-8-1.
Adding all nodes will mirror images.
5 + 4 = 9 7 + 2 = 9 8 + 9 = 17 1 + 1 = 2
To solve this problem, we will traverse the binary tree using inorder traversal. We will use two nodes, one to traverse the left subtree and the other to visit the mirror of the node. For example, we have a root node for the left subtree and for it we will have the mirrorroot that will traverse the mirror image of it.
Example
Program to illustrate the working of the solution,
#include <iostream> using namespace std; typedef struct node { int data; struct node* left; struct node* right; node(int d){ data = d; left = NULL; right = NULL; } } Node; void printMirrorSum(Node* root, Node* rootMirror){ if (root->left == NULL && rootMirror->right == NULL) return; printMirrorSum(root->left, rootMirror->right); cout<<(root->left->data + rootMirror->right->data)<<endl; printMirrorSum(root->right, rootMirror->left); } int main(){ Node* root = new Node(1); root->left = new Node(7); root->right = new Node(2); root->left->left = new Node(5); root->left->right = new Node(8); root->right->left = new Node(9); root->right->right = new Node(4); cout<<"Sum of node and mirror image nodes are :\n"; printMirrorSum(root, root); if (root) cout<<(root->data + root->data); return 0; }
Output
Sum of node and mirror image nodes are : 9 9 17 2
- Related Articles
- Program to perform an Inorder Traversal of a binary tree in Python
- Inorder Traversal of a Threaded Binary Tree in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Find sum of all nodes of the given perfect binary tree in C++
- Binary Tree Inorder Traversal in Python
- Count Complete Tree Nodes in C++
- Product of all nodes in a Binary Tree in C++
- Program to find Inorder Successor of a binary search tree in C++
- Replace Each Node in Binary Tree With The Sum Of Its Inorder Predecessor And Successor Using C++
- Check if an array represents Inorder of Binary Search tree or not in Python
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Program to find the largest sum of the path between two nodes in a binary tree in Python
- Print all internal nodes of a Binary tree in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
