Home > AI > Data Structure > Graph >

leetcode-1791-Find Center of Star Graph

Solution (Java)

class Solution {
    public int findCenter(int[][] edges) {
        int a = edges[0][0];
        int b = edges[0][1];
        int tmp = a;
        for (int i=1; i<edges.length; i++) {
           
            // duplicate
            if (edges[i][0] != a && edges[i][1] != a) {
                tmp = b;
                return tmp;
            }
            if (edges[i][0] != b && edges[i][1] != b) {
                tmp = a;
                return tmp;
            }
        }
        return tmp;
    }
}

Leave a Reply