Pairwise Swap leaf nodes in a binary tree
Given a binary tree, we need to write a program to swap leaf nodes in the given binary tree pairwise starting from left to right as shown below.
Tree before swapping:
Tree after swapping:
The sequence of leaf nodes in original binary tree from left to right is (4, 6, 7, 9, 10). Now if we try to form pairs from this sequence, we will have two pairs as (4, 6), (7, 9). The last node (10) is unable to form pair with any node and thus left unswapped.
The idea to solve this problem is to first traverse the leaf nodes of the binary tree from left to right. While traversing the leaf nodes, we maintain two pointers to keep track of first and second leaf nodes in a pair and a variable count to keep track of count of leaf nodes traversed. Now, if we observe carefully then we see that while traversing if the count of leaf nodes traversed is even, it means that we can form a pair of leaf nodes. To keep track of this pair we take two pointers firstPtr and secondPtr as mentioned above. Every time we encounter a leaf node we initialize secondPtr with this leaf node. Now if the count is odd, we initialize firstPtr with secondPtr otherwise we simply swap these two nodes.
Below is the C++ implementation of above idea:
CPP
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
void Swap(Node **a, Node **b)
{
Node * temp = *a;
*a = *b;
*b = temp;
}
Node **firstPtr;
Node **secondPtr;
void pairwiseSwap(Node **root, int &count)
{
if (!(*root))
return ;
if (!(*root)->left&&!(*root)->right)
{
secondPtr = root;
count++;
if (count%2 == 0)
Swap(firstPtr, secondPtr);
else
firstPtr = secondPtr;
}
if ((*root)->left)
pairwiseSwap(&(*root)->left, count);
if ((*root)->right)
pairwiseSwap(&(*root)->right, count);
}
Node* newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printInorder(Node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
printf ( "%d " , node->data);
printInorder(node->right);
}
int main()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(8);
root->right->left->left = newNode(6);
root->right->left->right = newNode(7);
root->right->right->left = newNode(9);
root->right->right->right = newNode(10);
cout << "Inorder traversal before swap:\n" ;
printInorder(root);
cout << "\n" ;
int c = 0;
pairwiseSwap(&root, c);
cout << "Inorder traversal after swap:\n" ;
printInorder(root);
cout << "\n" ;
return 0;
}
|
Java
class Node {
int data;
Node left, right;
Node( int data)
{
this .data = data;
left = null ;
right = null ;
}
}
class Main {
static Node firstPtr = null ;
static Node secondPtr = null ;
static int count = 0 ;
public static void pairWiseSwap(Node root)
{
if (root == null )
return ;
if (root.left == null && root.right == null ) {
secondPtr = root;
count++;
if (count % 2 == 0 ) {
int temp = firstPtr.data;
firstPtr.data = secondPtr.data;
secondPtr.data = temp;
}
else
firstPtr = secondPtr;
}
if (root.left != null )
pairWiseSwap(root.left);
if (root.right != null )
pairWiseSwap(root.right);
}
public static Node newNode( int data)
{
return new Node(data);
}
public static void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.right.left = newNode( 5 );
root.right.right = newNode( 8 );
root.right.left.left = newNode( 6 );
root.right.left.right = newNode( 7 );
root.right.right.left = newNode( 9 );
root.right.right.right = newNode( 10 );
System.out.print(
"Inorder traversal before swap : " );
printInorder(root);
pairWiseSwap(root);
System.out.println();
System.out.print( "Inorder traversal after swap: " );
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def Swap(a, b):
temp = a.data
a.data = b.data
b.data = temp
firstPtr = None
secondPtr = None
count = 0
def pairwiseSwap(root):
if (root is None ):
return
if (root.left is None and root.right is None ):
global count, firstPtr, secondPtr
secondPtr = root
count + = 1
if (count % 2 = = 0 ):
Swap(firstPtr, secondPtr)
else :
firstPtr = secondPtr
if (root.left is not None ):
pairwiseSwap(root.left)
if (root.right is not None ):
pairwiseSwap(root.right)
def newNode(data):
return Node(data)
def printInorder(node):
if (node is None ):
return
printInorder(node.left)
print (node.data, end = " " )
printInorder(node.right)
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.right.left = newNode( 5 )
root.right.right = newNode( 8 )
root.right.left.left = newNode( 6 )
root.right.left.right = newNode( 7 )
root.right.right.left = newNode( 9 )
root.right.right.right = newNode( 10 )
print ( "Inorder traversal before swap : " )
printInorder(root)
pairwiseSwap(root)
print ( "\nInorder traversal after swap : " )
printInorder(root)
|
C#
using System;
public class Node{
public int data;
public Node left, right;
public Node( int item){
data = item;
left = right = null ;
}
}
public class BinaryTree{
static Node firstPtr;
static Node secondPtr;
static void Swap(Node a, Node b){
int temp = a.data;
a.data = b.data;
b.data = temp;
}
static int count = 0;
static void pairwiseSwap(Node root){
if (root == null ) return ;
if (root.left == null && root.right == null ){
secondPtr = root;
count += 1;
if (count % 2 == 0){
Swap(firstPtr, secondPtr);
} else {
firstPtr = secondPtr;
}
}
if (root.left != null ){
pairwiseSwap(root.left);
}
if (root.right != null ){
pairwiseSwap(root.right);
}
}
static Node newNode( int data){
return new Node(data);
}
static void printInorder(Node node){
if (node == null ) return ;
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
static public void Main (){
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(8);
root.right.left.left = newNode(6);
root.right.left.right = newNode(7);
root.right.right.left = newNode(9);
root.right.right.right = newNode(10);
Console.WriteLine( "Inorder Traversal before swap : " );
printInorder(root);
Console.WriteLine();
pairwiseSwap(root);
Console.WriteLine( "Inorder traversal after swap : " );
printInorder(root);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
function Swap(a, b){
let temp = a.data;
a.data = b.data;
b.data = temp;
}
let firstPrt;
let secondPtr;
let count = 0;
function pairwiseSwap(root)
{
if (root == null ) return ;
if (root.left == null && root.right == null ){
secondPtr = root;
count++;
if (count % 2 == 0){
Swap(firstPtr, secondPtr);
}
else {
firstPtr = secondPtr;
}
}
if (root.left != null ){
pairwiseSwap(root.left);
}
if (root.right != null ){
pairwiseSwap(root.right);
}
}
function newNode(data){
return new Node(data);
}
function printInorder(node){
if (node == null )
return ;
printInorder(node.left);
console.log(node.data + " " );
printInorder(node.right);
}
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(8);
root.right.left.left = newNode(6);
root.right.left.right = newNode(7);
root.right.right.left = newNode(9);
root.right.right.right = newNode(10);
console.log( "Inorder traversal before swap: " );
printInorder(root);
pairwiseSwap(root);
console.log( "Inorder traversal after swap: " );
printInorder(root);
|
Output
Inorder traversal before swap:
4 2 1 6 5 7 3 9 8 10
Inorder traversal after swap:
6 2 1 4 5 9 3 7 8 10
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...