Given the binary Tree of size N and the two-node value say ‘a’ and ‘b’, determine whether the two-node values are cousins of each other or not. The values of all the nodes in the tree are distinct.
Two nodes value are cousins of each other if they are at the same level and have different parents.
Input:
The first line of input contains the number of test cases T. For each test case, there will be two lines of input, First input is a string representing the tree as described below and the second line contains two space-separated integers denoting node values:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
For example:
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Output:
Single line output, print "1" if they are cousins else "0".
Your Task:
You don't need to take input. Just complete the function isCousins() that accepts the root node of the tree, and two integers as a parameter and returns true, if two integers are cousins else returns false.
Constraints:
1<=T<=1000
1<=N<=1000
Example:
Input:
2
1 2 3
2 3
1 2 3 5 N N 4
4 5
Output:
0
1
Note: The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
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.
SumitSingh27 | 72 |
SanketAgarwal1 | 54 |
rathiarpit29 | 52 |
ujjwalsoni1999 | 52 |
vishalkumar61 | 50 |
SumitSingh27 | 398 |
saiujwal13083 | 279 |
ujjwalsoni1999 | 277 |
NaveenKumarNakka | 275 |
_maverick7_ | 266 |
blackshadows | 5362 |
Ibrahim Nash | 5242 |
akhayrutdinov | 5111 |
mb1973 | 4931 |
Quandray | 4598 |