FunctionRacerProblemsRace

Next Door

BeginnerGraphsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems