Sum of the mirror image nodes of a complete binary tree in an inorder way
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.
Examples:
Input:
Output:
20
51
19
10
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
C++
#include <iostream>
using namespace std;
typedef struct node {
int data;
struct node* l;
struct node* r;
node( int d)
{
data = d;
l = NULL;
r = NULL;
}
} Node;
void printInorder(Node* rootL, Node* rootR)
{
if (rootL->l == NULL && rootR->r == NULL)
return ;
printInorder(rootL->l, rootR->r);
cout << rootL->l->data + rootR->r->data << endl;
printInorder(rootL->r, rootR->l);
}
int main()
{
Node* root = new Node(5);
root->l = new Node(23);
root->r = new Node(28);
root->l->l = new Node(4);
root->l->r = new Node(11);
root->r->l = new Node(8);
root->r->r = new Node(16);
printInorder(root, root);
if (root)
cout << root->data * 2 << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node l;
Node r;
Node( int d)
{
data = d;
l = null ;
r = null ;
}
}
static void printInorder(Node rootL, Node rootR)
{
if (rootL.l == null && rootR.r == null )
return ;
printInorder(rootL.l, rootR.r);
System.out.println(rootL.l.data + rootR.r.data );
printInorder(rootL.r, rootR.l);
}
public static void main(String args[])
{
Node root = new Node( 5 );
root.l = new Node( 23 );
root.r = new Node( 28 );
root.l.l = new Node( 4 );
root.l.r = new Node( 11 );
root.r.l = new Node( 8 );
root.r.r = new Node( 16 );
printInorder(root, root);
if (root != null )
System.out.println(root.data * 2 );
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self .l = None
self .r = None
def printInorder(rootL, rootR):
if rootL.l = = None and rootR.r = = None :
return
printInorder(rootL.l, rootR.r)
print (rootL.l.data + rootR.r.data)
printInorder(rootL.r, rootR.l)
if __name__ = = "__main__" :
root = Node( 5 )
root.l = Node( 23 )
root.r = Node( 28 )
root.l.l = Node( 4 )
root.l.r = Node( 11 )
root.r.l = Node( 8 )
root.r.r = Node( 16 )
printInorder(root, root)
if root:
print (root.data * 2 )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node l;
public Node r;
public Node( int d)
{
data = d;
l = null ;
r = null ;
}
}
static void printInorder(Node rootL, Node rootR)
{
if (rootL.l == null && rootR.r == null )
return ;
printInorder(rootL.l, rootR.r);
Console.WriteLine(rootL.l.data + rootR.r.data );
printInorder(rootL.r, rootR.l);
}
public static void Main(String []args)
{
Node root = new Node(5);
root.l = new Node(23);
root.r = new Node(28);
root.l.l = new Node(4);
root.l.r = new Node(11);
root.r.l = new Node(8);
root.r.r = new Node(16);
printInorder(root, root);
if (root != null )
Console.WriteLine(root.data * 2 );
}
}
|
Javascript
<script>
class Node
{
constructor(d)
{
this .l = null ;
this .r = null ;
this .data = d;
}
}
function printInorder(rootL, rootR)
{
if (rootL.l == null && rootR.r == null )
return ;
printInorder(rootL.l, rootR.r);
document.write(rootL.l.data +
rootR.r.data + "</br>" );
printInorder(rootL.r, rootR.l);
}
let root = new Node(5);
root.l = new Node(23);
root.r = new Node(28);
root.l.l = new Node(4);
root.l.r = new Node(11);
root.r.l = new Node(8);
root.r.r = new Node(16);
printInorder(root, root);
if (root != null )
document.write(root.data * 2 );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
05 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...