Geeksforgeeks

Error

×

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 | |

Handle | Score |

@cfwong8 | 1156 |

@gurshehzadsingh | 676 |

@nithinreddy3210 | 642 |

@jha8768 | 577 |

@Run_for_life | 574 |

@divyagupta221100 | 557 |

@AkashLahoty | 551 |

@aayushdesai01 | 526 |

@riyakumaribkj | 526 |

@2017akku | 521 |

@amansinha74458 | 503 |

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 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 7Output: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<=10^{3}

1<=Data of a node <=10^{3}

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?

YesLoading...

Exchange the Leaf Nodes

...