Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 662 |

@arpitsatnalika | 629 |

@code_it_ | 591 |

@prakharxyz | 583 |

@tujhekya | 542 |

@Rahul Jadhav 1 | 458 |

@sivanandh03 | 448 |

@satyam0460 | 433 |

@iamronak | 424 |

@imnm | 424 |

@18eumc107 | 421 |

Complete Leaderboard |

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:

```
Tree before swapping:
Tree after swapping:
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
```

**Input:**

The function takes a single arguemnt as input, the reference pointer to the **root **of the binary Tree.

There will be **T**, test cases and for each test case the function will be called separately.

**Output:**

For each test case print the inorder traversal of the new tree on a new line.

**Constraints:**

1<=T<=10^{3}

1<=N<=10^{3}

**Example:
Input:**

1

9

1 2 L 2 4 L 1 3 R 3 5 L 3 8 R 5 6 L 5 7 R 8 9 L 8 10 R

6 2 1 4 5 9 3 7 8 10

Login to report an issue on this page.

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

Yes
Exchange the Leaf Nodes

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.