Skip to content

1791. Find Center of Star Graph 👍

  • Time: $O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
class Solution {
 public:
  int findCenter(vector<vector<int>>& edges) {
    return edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1]
               ? edges[0][0]
               : edges[0][1];
  }
};
1
2
3
4
5
6
7
class Solution {
  public int findCenter(int[][] edges) {
    return edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] //
        ? edges[0][0]
        : edges[0][1];
  }
}
1
2
3
4
5
class Solution:
  def findCenter(self, edges: list[list[int]]) -> int:
    if edges[0][0] in edges[1]:
      return edges[0][0]
    return edges[0][1]