[算法] Search Graph Nodes

Question
Search Graph Nodes

Question Description

Given a undirected graph, a node and a target, return the nearest node to given node which value of it is target, return NULL if you can't find.
There is a mapping store the nodes' values in the given parameters.Find any topological order for the given graph.
Language
Python 3

Data structure description

""" @param: graph: a list of Undirected graph node @param: values: a hash mapping, <UndirectedGraphNode, (int)value> @param: node: an Undirected graph node @param: target: An integer @return: a node """

Solution

class Solution: def searchNode(self, graph, values, node, target): if values[node] == target: return node queue = collections.deque([node]) while queue: curr_node = queue.popleft() for neighbor in curr_node.neighbors: if values[neighbor] == target: return neighbor queue.append(neighbor) return None

Reference Link

https://www.lintcode.com/problem/search-graph-nodes/description

留言

熱門文章