Well, I didn’t look at the code, but the second test case invloved a case where the root (vertex 1) only had one link.
In my code, I checked if the number of links were 1, and if it was, then i decided that it was a leaf node. I didn’t consider that the root can have only 1 link too.
But then i was able to get PA after rectifying that.
Maybe that’s where you are getting stuck up?