Next Door
An undirected graph has n nodes numbered 0 to n - 1. edges is a list of pairs [a, b] with a != b; each pair is an edge that joins a and b, which makes each of them a neighbour of the other. The same pair may appear more than once, in either order.
Return the neighbours of node node in increasing order, listing each neighbour once. Return [] when node has no neighbours.
Example 1
n 5
edges [[0,1],[1,2],[3,1],[1,0]]
node 1
returns [0,2,3]
Node 1 shares an edge with 0 (twice), 2 and 3; node 0 is listed once.
Example 2
n 4
edges [[0,1],[2,3]]
node 0
returns [1]
The only edge at node 0 leads to node 1.
Constraints
1 <= n <= 500 <= len(edges) <= 1000 <= a, b <= n - 1anda != b0 <= node <= n - 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- No HandshakeBeginner
- Who Points at MeBeginner
- Busiest NodeEasy
- Cable GroupsEasy
- Count the LakesEasy
- Everything Needed FirstEasy
- Everything ReachableEasy
- Farthest VillageEasy