Dare2Solve
A star graph has one central node that is connected to all other nodes. Therefore, any two edges in the graph must share this central node. By examining the nodes in the first two edges, we can determine which node is the center.
class Solution {
public:
int findCenter(vector<vector<int>>& edges) {
int a = edges[0][0];
int b = edges[0][1];
int c = edges[1][0];
int d = edges[1][1];
return (a == c || b == c) ? c : d;
}
};
class Solution:
def findCenter(self, edges: List[List[int]]) -> int:
a, b = edges[0]
c, d = edges[1]
return c if a == c or b == c else d
class Solution {
public int findCenter(int[][] edges) {
int a = edges[0][0];
int b = edges[0][1];
int c = edges[1][0];
int d = edges[1][1];
return (a == c || b == c) ? c : d;
}
}
/**
* @param {number[][]} edges
* @return {number}
*/
var findCenter = function (edges) {
var [a, b] = edges[0];
var [c, d] = edges[1];
return a === c || b === c ? c : d;
};