题目

类型:图
image.png

解题思路

根据题意,中心节点必然出现在所有的 edges[i] 中,因此使用前两条边即可确定答案。

起始让 edges[0][0] 和 edges[0][1] 作为答案候选,然后在 edges[1] 关系中检查哪个候选出现过。

代码

  1. class Solution {
  2. public int findCenter(int[][] edges) {
  3. int a = edges[0][0], b = edges[0][1];
  4. if (a == edges[1][0] || a == edges[1][1]) return a;
  5. else return b;
  6. }
  7. }