[LeetCode] Find Center of Star Graph

1791. Find Center of Star Graph

There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.

You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph.

1
2
3
4
5
6
7
8
class Solution {
public:
int findCenter(vector<vector<int>>& edges) {
int res1 = edges[0][0], res2 = edges[0][1];
if(edges[1][0] == res1 or edges[1][1] == res1) return res1;
return res2;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/27/PS/LeetCode/find-center-of-star-graph/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.