Leaderboard
Showing:
Next Right Node
Easy Accuracy: 32.33% Submissions: 3242 Points: 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

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.

Next Right Node

Output Window