Find center of star graph

December 5, 2022

graph

Problem URL: Find center of star graph

As it's a start graph, each edge will be connected to the center. So we can just check the first 2 edges and return the node that is connected to both of them.

class Solution:
    def findCenter(self, edges: List[List[int]]) -> int:
        if edges[0][0]==edges[1][0] or edges[0][0]==edges[1][1]:
            return edges[0][0]
        return edges[0][1]

Time complexity: O(1)
Space complexity: O(1)