ServiceNow | OA | Tree Question
Anonymous User
2887

ServiceNow Women Code to Win Contest-2022 was conducted on 11 Feb and this question appeared in the test.
There is tree in which root is colored black and other remaining nodes are colored red. we can color adjacent node to black and we need to return total ways through which we can color the tree black.
Eg, Input- Total no of nodes in tree, total no of edges
from node array
to node array
4 3
1 1 1
2 3 4
Output - 6
Explanation - so according to que tree is like this
1 -- black
/ | \
2 3 4 -- all red
this tree can be colored as all black by 6 ways
1. 2 is adjacent to 1 (which is black) so color 2 then 3 then 4.
2. color in order 2 then 4 then 3 all nodes are black now.
3. color in order 3 then 2 then 4 all nodes are black now.
4. color in order 3 then 4 then 2 all nodes are black now.
5. color in order 4 then 2 then 3 all nodes are black now.
6. color in order 4 then 3 then 2 all nodes are black now.
So these are all 6 ways and thus we return 6 as answer.

		Eg 2.   5 4
		           1 2 3 3
				   2 3 4 5
				   output - 2
				   

How can we solve this question any help will be appreciated. Thanks for reading.

Comments (3)