Problem of the day
Let’s say we have n=4 nodes, 'LEFT_CHILD' = {1, -1, 3, -1} and
RIGHT_CHILD = {2, -1, -1, -1}. So the resulting tree will look like this:
It will return True as there is only one valid binary tree and each node has only one parent and there is only one root.
The very first line of input contains an integer ‘T’ denoting the
number of test cases.
The first line of every test case contains an integer ‘N’ denoting
the number of nodes of the tree.
The next two lines of every test case contain the 'LEFT_CHILD' array and
'RIGHT_CHILD' array respectively.
For each test case, return either ‘Yes’ or ‘No’ that whether from given nodes, we can form exactly one valid binary tree or not.
Output for each test case is printed on a separate line.
You do not need to print anything, it has already been taken care of. Just return ‘True’ or ‘False’ that whether from given nodes we can form exactly one valid binary tree or not.
The nodes have no values and that we only use the node numbers in this problem.
1 <= T <= 10
1 <= N <= 10^4
LEFT_CHILD.length == RIGHT_CHILD.length == N
-1 <= LEFT_CHILD[i], RIGHT_CHILD[i] <= N - 1
Time Limit: 1 sec
2
4
1 -1 3 -1
2 -1 -1 -1
4
1 -1 3 -1
2 3 -1 -1
Yes
No
For the first test case,
It is already explained above in the example.
For the second test case,
The resulting tree from the given input will be :
So the output will be ‘False’ because node 3 has two parents 1
and 2.
2
2
1 0
-1 -1
6
1 -1 -1 4 -1 -1
2 -1 -1 5 -1 -1
No
No