BFS-树
BFS+树
Alwaysion
more persistent for nothing
个人qq:790693499,欢迎来交流
展开
-
【力扣752. 打开转盘锁】双向BFS(python3)
题目描述https://leetcode-cn.com/problems/open-the-lock/思路题解双向bfs https://leetcode-cn.com/problems/open-the-lock/solution/gong-shui-san-xie-yi-ti-shuang-jie-shuan-wyr9/class Solution: def openLock(self, deadends: List[str], target: str) -> int:原创 2021-08-07 15:51:57 · 201 阅读 · 0 评论 -
【力扣103. 二叉树的锯齿形层序遍历】BFS双端队列(多种解法)(python3)
题目描述https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/思路题解自己写的方法,层序遍历后按层数逆转class Solution: def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: if not root:return [] from collections import deque原创 2021-08-05 14:54:22 · 117 阅读 · 0 评论 -
【力扣剑指 Offer 13. 机器人的运动范围】bfs可达性分析(python3)
题目描述https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/思路题解https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/solution/mian-shi-ti-13-ji-qi-ren-de-yun-dong-fan-wei-dfs-b/from collections import dequeclass Solution:原创 2021-07-13 17:31:52 · 217 阅读 · 0 评论 -
【力扣207. 课程表】bfs拓扑排序+dfs 判断是否有向图是否存在环(Python3)
题目描述https://leetcode-cn.com/problems/course-schedule/思路题解https://leetcode-cn.com/problems/course-schedule/solution/course-schedule-tuo-bu-pai-xu-bfsdfsliang-chong-fa/BFS拓扑排序from collections import dequeclass Solution: def canFinish(self, numCour原创 2021-07-01 17:33:41 · 466 阅读 · 0 评论 -
【力扣279. 完全平方数】完全背包+数学法-四平方和定理+BFS(python3)
题目描述https://leetcode-cn.com/problems/perfect-squares/思路题解完全背包一开始的思路:n=6665的时候,时间超限class Solution: def numSquares(self, n: int) -> int: if n<4:return n dp=[10001]*(n+1) dp[1],dp[2],dp[3],dp[4]=1,2,3,1 for i in原创 2021-06-28 17:56:43 · 192 阅读 · 0 评论 -
【力扣200. 岛屿数量 305. 岛屿数量 II】DFS+BFS+并查集、并查集(python3)
题目描述https://leetcode-cn.com/problems/number-of-islands/思路题解https://leetcode-cn.com/problems/number-of-islands/solution/number-of-islands-shen-du-you-xian-bian-li-dfs-or-/原创 2021-06-18 15:05:49 · 502 阅读 · 0 评论 -
【力扣199-二叉树的右视图】BFS、右序DFS双方法(Python3)
目录题目描述思路及代码题目描述链接:https://leetcode-cn.com/problems/binary-tree-right-side-view/思路及代码原来想的是用BFS遍历,用一个list保存层次遍历的节点,将所有遍历到的节点(包含空节点)存入list中,最后通过计算的方法,从2i~2(i-1)里面选取最右侧的节点输出。但是这个方法时间较慢,而且比较笨,遂根据答案提示,写了下新的两种方法。方法一:BFSbfs遍历队列,用duque保存节点和深度,最后用dict保存各个深度下的最原创 2020-11-20 14:09:08 · 219 阅读 · 1 评论