Next Right Node
Submissions: 1386   Accuracy:

32.57%

  Difficulty: Easy   Marks: 2

Given a Binary tree and a key in the binary tree, find the node right to the given key. If there is no node on right side, then return -1.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the number of edges. The next line contains the edges of the tree. The last line of input contains the key.

Output:
Print the node right to the given key. If there is no node on right side, then return -1.

Constraints:
1<=T<=1000
1<=n<=1000
1<=data of node<=1000
1<=key<=1000

Example:
Input:

2
5
10 2 L 2 8 L 2 4 R 10 6 R 6 5 R
2
5
10 2 L 2 8 L 2 4 R 10 6 R 6 5 R
4

Output:
6
5

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Ayush Govil
Author: Ayush Govil 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.