Stepping_Oline_Judge_BFS

Lintcode--Leetcode: Base + Senior


BFS-- level order traversal


经典模板:
总结:使用queue作为维护每一层节点,并由上一层拓展到下一层的数据结构:
  1. 遍历一个节点所在层,队尾加入
  2. 队首pop,加入pop节点的儿子们,由x层拓展到x+1层。size动态变化。


连通块Connected Component问题:


图的表示:邻接表
lintcode:
Similar leetcode:


Lintcode--Leetcode: Base + Senior

BFS in Binary Tree

经典模板:
总结:使用queue作为维护每一层节点,并由上一层拓展到下一层的数据结构:
  1. 遍历一个节点所在层,队尾加入
  2. 队首pop,加入pop节点的儿子们,由x层拓展到x+1层。size动态变化。

连通块Connected Component问题:

图的表示:邻接表
lintcode:
Similar leetcode:

图中的bfs:
图和矩阵中,节点之间是双向关系,所以需要一个boolean矩阵,记录是否访问过,防止节点重复进入queue。

图和矩阵的bfs,for循环在外部
对比:
右边,bfs in graph, 相当于只做了一层的遍历

矩阵中的bfs:
变形:number of island
Friend circles


总结不同数据结构的bfs:
  1. tree bfs, 2 directions, left & right 父亲到左右儿子
  2. matrix bfs, 4 directions, using dx,dy (除边界)节点的上下左右
  3. graph bfs, all neighbors 节点到邻居

序列化问题:
Leetcode 序列化bst 和 bt
Lintcode


变形:number of island


序列化问题:
Leetcode 序列化bst 和 bt
Lintcode

评论

此博客中的热门博文

8 Link Analysis

1 Map reduce problems

NoSql and AWS DynamoDB practices