The title

There is an undirected star diagram consisting of n nodes numbered from 1 to N. The star diagram has a central node and has exactly n-1 edges connecting the central node to every other node.

Edges give you a two-dimensional integer array where edges[I] = [UI, vi] indicate that there is an edge between the node UI and vi. Please find and return the center node of the star graph represented by the edges.

Example 1: input: edges = [[1,2],[2,3],[4,2]] output: 2 explanation: as shown in the figure above, node 2 is connected to every other node, so node 2 is the central node. Example 2: input: edges = [[1,2],[5,1],[1,3],[1,4]] output: 1Copy the code

Tip:

3 <= n <= 105 edges.length == n – 1 edges[i].length == 2 1 <= ui, vi <= n ui ! = vi The data given by the problem represent a valid star graph

Their thinking

class Solution: def findCenter(self, edges: List[List[int]]) -> int: ResList = [] # Disassemble all the array to form a List, then use Counter to generalize, find n for I in edges: resList += i # print(resList) from collections import Counter count = Counter(resList) for (key, val) in count.items(): # print(key,val) # print(len(edges)-1) if val == (len(edges)): return key return 0 if __name__ == '__main__': Edges = [[1, 2], [2, 3], [4, 2]] ret = Solution () findCenter (edges) print (ret)Copy the code