Rotate Array
You are given an array of N length. You have to rotate the array right...
Max. Marks: 6
Solved By : 750
Floating Number
Bob has a floating point number N. He wants to set the precision for 2...
Max. Marks: 4
Solved By : 4383
SwapMaster solves Symmetric Swap
The SwapMaster is known to be the greatest and fastest swapper of all ...
Max. Marks: 4
Solved By : 3857
String Matching
Cody has a sequence of characters N. He likes a sequence if it contain...
Max. Marks: 3
Solved By : 2045
Leap Year
Steve is playing a quiz game with his brother John. As Steve just lear...
Max. Marks: 6
Solved By : 3213
Project Teams
There are N students in a class and Teacher want to divide these stude...
Max. Marks: 5
Solved By : 3993
Circle of Numbers
All numbers in NumberLand are standing in a circle for a dancing cerem...
Max. Marks: 6
Solved By : 2759
Happy String
A happy string is a string in which each character is lexicographicall...
Max. Marks: 4
Solved By : 2050
Degree Celsius
Tom is a scientist. He uses huge machines for complex calculations. Th...
Max. Marks: 4
Solved By : 4654
Three's Company
This problem requires you to create a output string from input string ...
Max. Marks: 4
Solved By : 3068
Looking in the Mirror
Please select a challenge
Problem Statement
Greg is given a binary tree rooted at X. He is tasked by his supervisor to find the mirror node of any node 't' with respect to X. He doesn't want to disappoint his supervisor and has asked you for help. Please help him find the mirror node of a given node 't' with respect to the root node. If the mirror node doesn't exist, print 'Not Exist'. NOTE : It is guaranteed that node 't' exists in the tree

The first line of input contains 3 variables, N, Q and X. Next N lines each contain two integers and one character, first integer represents parent node, second integer represents child node and third character 'L' or 'R' represent left child or right child. (Use these instructions to insert child node in binary tree). Next Q lines each contain a query, an integer, representing node 't'.

For each query, print the mirror node of 't', if not found, print 'Not Exist' in a newline.

1 ≤ N ≤ 1000 1 ≤ Q ≤ 1000 -1000 ≤ nodes(including X) ≤ 1000 It is guaranteed that there are no duplicate values in the tree.

Sample Input
7 3 2
2 3 R
2 4 L
3 5 R
3 6 L
4 7 R
4 8 L
5 9 L

Sample Output

Not Exist

Dcoded By: Mrudul Sankhere

Solved By: 154

Maximum Marks: 20