127. Topological Sorting

2019-05-30  本文已影响0人  鸭蛋蛋_8441

Description

Given an directed graph, a topological order of the graph nodes is defined as follow:

For each directed edge A -> B in graph, A must before B in the order list.

The first node in the order can be any node in the graph with no nodes direct to it.

Find any topological order for the given graph.

You can assume that there is at least one topological order in the graph.

Clarification

Learn more about representation of graphs

Example

For graph as follow:

The topological order can be:

[0, 1, 2, 3, 4, 5]

[0, 2, 3, 1, 5, 4]

...

Challenge

Can you do it in both BFS and DFS?

思路:

套路很简单,记住就可以了,就是一个简单的拓扑排序

代码:

上一篇 下一篇

猜你喜欢

热点阅读