LeetCode 210 Course Schedule II - Solution

The core idea

This is Course Schedule I but you also need to output the order.

Use Kahn's algorithm. As you process each course (when its in-degree becomes 0), add it to the result list.

If the result list has fewer than numCourses items at the end, a cycle exists and no valid ordering is possible.

The order in which courses reach in-degree 0 is a valid topological order.