搜索

什么情况下用BFS?

  • 图的遍历
    • 层级遍历 Level Order Traversal
    • 由点及面 Connected Component
    • 拓扑排序 Topological Sorting
  • 最短路径
    • 简单图的最短路径,也就是图中每条边的长度都是1,且没有方向

什么情况下用DFS?

  • 找所有的方案

练习题

https://leetcode.com/problems/combination-sum/description

https://leetcode.com/problems/combination-sum-ii/description

https://leetcode.com/problems/palindrome-partitioning

https://leetcode.com/problems/permutations/description

https://leetcode.com/problems/permutations-ii/description

Scroll to Top