Search Nearest Target Node

Question (LI.618)

Given a node in an undirected graph and a target, find the nearest node that is the target. If the target doesn't exist in the graph, return null.

Example

I: node_1, 50
2------3  5
 \     |  | 
  \    |  |
   \   |  |
    \  |  |
      1 --4

{node_1: 3, node_2: 4, node_3: 10, node_4: 50, node_5: 50}
O: node_4

Approach

We want to find the nearest node from a given node. BFS is the perfect candidate for that job.

Code

Last updated