Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6420
@blackshadows 6380
@mb1973 5704
@Quandray 5245
@akhayrutdinov 5111
@saiujwal13083 5046
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3523
@mantu_singh 3510
@sushant_a 3459
Complete Leaderboard
Exchange the Leaf Nodes
Easy Accuracy: 34.69% Submissions: 7780 Points: 2

Given a binary tree of size N, your task is to complete the function pairwiseSwap(), that swap leaf nodes in the given binary tree pairwise starting from from left to right.
Example 1:

Input: 


Output: 7 2 1 4 5 9 3 8 6 10 

Explanation:
The sequence of leaf nodes in original binary tree
from left to right is (4, 7, 8, 9, 10). Now if we 
try to form pairs from this sequence, we will have 
two pairs as (4, 7), (8, 9). The last node (10) is 
unable to form pair with any node and thus left unswapped

 

Example 2:

Input: 
          1
       /     \
      2       3
       \    /    \
        5  6      7
Output: 2 6 1 5 3 7
        1
     /     \
    2       3
     \    /   \
      6  5     7

 

Your Task:
You don't need to read input or print anything. Your task is to complete the function pairwiseSwap() which takes root node of the tree as input parameter.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1<=N<=103
1<=Data of a node <=103

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Exchange the Leaf Nodes

Output Window