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:

`/* C++ program to pairwise swap` ` ` `leaf nodes from left to right */` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// A Binary Tree Node` `struct` `Node` `{` ` ` `int` `data;` ` ` `struct` `Node *left, *right;` `};` ` ` `// function to swap two Node` `void` `Swap(Node **a, Node **b)` `{` ` ` `Node * temp = *a;` ` ` `*a = *b;` ` ` `*b = temp;` `}` ` ` `// two pointers to keep track of` `// first and second nodes in a pair` `Node **firstPtr;` `Node **secondPtr;` ` ` `// function to pairwise swap leaf` `// nodes from left to right` `void` `pairwiseSwap(Node **root, ` `int` `&count)` `{` ` ` `// if node is null, return` ` ` `if` `(!(*root))` ` ` `return` `;` ` ` ` ` `// if node is leaf node, increment count` ` ` `if` `(!(*root)->left&&!(*root)->right)` ` ` `{` ` ` `// initialize second pointer` ` ` `// by current node` ` ` `secondPtr = root;` ` ` ` ` `// increment count` ` ` `count++;` ` ` ` ` `// if count is even, swap first` ` ` `// and second pointers` ` ` `if` `(count%2 == 0)` ` ` `Swap(firstPtr, secondPtr);` ` ` ` ` `else` ` ` ` ` `// if count is odd, initialize` ` ` `// first pointer by second pointer` ` ` `firstPtr = secondPtr;` ` ` `}` ` ` ` ` `// if left child exists, check for leaf` ` ` `// recursively` ` ` `if` `((*root)->left)` ` ` `pairwiseSwap(&(*root)->left, count);` ` ` ` ` `// if right child exists, check for leaf` ` ` `// recursively` ` ` `if` `((*root)->right)` ` ` `pairwiseSwap(&(*root)->right, count);` ` ` `}` ` ` `// Utility function to create a new tree node` `Node* newNode(` `int` `data)` `{` ` ` `Node *temp = ` `new` `Node;` ` ` `temp->data = data;` ` ` `temp->left = temp->right = NULL;` ` ` `return` `temp;` `}` ` ` `// function to print inorder traversal` `// of binary tree` `void` `printInorder(Node* node)` `{` ` ` `if` `(node == NULL)` ` ` `return` `;` ` ` ` ` `/* first recur on left child */` ` ` `printInorder(node->left);` ` ` ` ` `/* then print the data of node */` ` ` `printf` `(` `"%d "` `, node->data);` ` ` ` ` `/* now recur on right child */` ` ` `printInorder(node->right);` `}` ` ` `// Driver program to test above functions` `int` `main()` `{` ` ` `// Let us create binary tree shown in` ` ` `// above diagram` ` ` `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);` ` ` ` ` `// print inorder traversal before swapping` ` ` `cout << ` `"Inorder traversal before swap:\n"` `;` ` ` `printInorder(root);` ` ` `cout << ` `"\n"` `;` ` ` ` ` `// variable to keep track` ` ` `// of leafs traversed` ` ` `int` `c = 0;` ` ` ` ` `// Pairwise swap of leaf nodes` ` ` `pairwiseSwap(&root, c);` ` ` ` ` `// print inorder traversal after swapping` ` ` `cout << ` `"Inorder traversal after swap:\n"` `;` ` ` `printInorder(root);` ` ` `cout << ` `"\n"` `;` ` ` ` ` `return` `0;` `}` |

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( 1 )

This article is contributed by **Harsh Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.