Such statements are useless. Because almost always, people don't measure properly. So such statements can't be trusted. How much faster, and how consistently? How often did you submit each? What were the individual times? What were the average times?
@hollysinka This is definitely not O(n) solution. Please mark the complexity in the title, in order to make people not confused. For every node, you did isValid() and count() for it. This means for every node, you traverse all the nodes belong to this node, which is O(n). That is to say, you did O(n) for each node. So, when you finished the calculation, you did O(n) * n = O(n^2).