1.二叉树 BFS

2.拓扑排序  重点 BFS

3.棋盘上的宽搜  BFS

图的遍历

层级遍历,由点及面,拓扑排序,简单图的最短路径

如果题目问最短路径:可能是BFS或者DP, 最长路径:DFS

queue 的数组实现

1.二叉树的BFS

https://www.lintcode.com/problem/binary-tree-level-order-traversal/description

 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
// write your code here
List<List<Integer>> results = new ArrayList<>();
if(root == null){
return results;
}
//interface offer(add) poll(pop)
Queue<TreeNode> queue = new LinkedList<>();
//put start node into queue
queue.offer(root); while(!queue.isEmpty()){
//lever x->x+1
ArrayList<Integer> currentLevel = new ArrayList();
int size = queue.size();
for(int i =0; i<size;i++){
TreeNode head = queue.poll();
currentLevel.add(head.val);
if(head.left != null){
queue.offer(head.left);
}
if(head.right != null){
queue.offer(head.right);
}
}
results.add(currentLevel);
} return results; }
}

queue, offer 和 add的区别

序列化:

xml json thrift(by facebook) protobuf(by google)

序列化算法设计时需要考虑的因素:压缩率 thrift protobuf是为了更快的传输数据和节省存储空间而设计的

可读性:如json

https://www.lintcode.com/problem/graph-valid-tree/description

 public class Solution {
/**
* @param n: An integer
* @param edges: a list of undirected edges
* @return: true if it's a valid tree, or false
*/
public boolean validTree(int n, int[][] edges) {
// write your code here
if(n==0){
return false;
} if(edges.length != n-1){
return false;
}
//adjacent lists
Map<Integer,Set<Integer>> graph = initializeGraph(n,edges); Queue<Integer> queue = new LinkedList<>();
Set<Integer> hash = new HashSet<>(); queue.offer(0);
hash.add(0); while(!queue.isEmpty()){
int node = queue.poll();
for(Integer neigh:graph.get(node)){
if(hash.contains(neigh)){
continue;
}
hash.add(neigh);
queue.offer(neigh);
}
}
return hash.size()==n; } private Map<Integer,Set<Integer>> initializeGraph(int n,int[][]edges){
Map<Integer,Set<Integer>> graph = new HashMap<>();
for(int i=0;i<n;i++){
graph.put(i,new HashSet<>());
}
for(int i =0;i<edges.length;i++){
int u = edges[i][0];
int v = edges[i][1];
graph.get(u).add(v);
graph.get(v).add(u);
}
return graph;
}
}

BFS - 20190206的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. js中的函数参数问题

    js函数没有Java中的重载现象.js函数的参数是放在arguments的容器里面的. <script  type="text/javascript"> functio ...

  2. 开发工具Visual Studio使用相关知识和经验的碎片化记录

    开发工具Visual Studio使用相关知识和经验的碎片化记录 1.Visual Studio提示"无法启动IIS Express Web服务器"的解决方法 有时,在使用Visu ...

  3. Requests接口测试-对cookies的操作处理(二)

    我们继续来讨论一下cookie这方面的内容,我们都知道cookie是数据,一般的话在我接口测试中,数据都是要和代码进行分离的.本篇内容,我们队cookie内容进行处理,我们把登陆成功后的cookie写 ...

  4. linux常用Java程序员使用命令(一)

    pwd 显示当前路径cd 切换目录 . .. ~ls 显示文件(夹) -l 显示详细信息 -a 显示全部,包括隐藏文件(夹) mkdir 创建文件夹 -p 递归创建 touch 创建空白文件 echo ...

  5. Spring框架总结(一)

    名词解释: 框架就是组件的集合.比如:Struts.Spring.Hibernate就是组件的集合 组件就是常用的功能包封装成工具类. 常用组件: Dom4j/Xpath.DBUtils.C3p0.B ...

  6. 大前端涉猎之前后端交互总结2:使用PHP进行表单数据上传与更新

    1:使用PHP进行表单上传 1.1 form表单的数据收集 HTML页面: 代码解释:核心模块是form的属性: --提交方式 :  method="post" --指定 name ...

  7. linux命令的笔记

    1.改变目录的用户组和所有者 chown 命令 如下图: 可以看到test1与test2的的所有者和所属组都是root,其中 第三个字段是说明目录拥有者, 第四个字段是文件拥有者所在的组, 第五个字段 ...

  8. 编写高质量代码改善C#程序的157个建议——建议126:用名词和名词组给类型命名

    建议126:用名词和名词组给类型命名 类型对应着现实世界中的实际对象.对象在语言中意味着它是一个名词.所以,类型也应该以名词或名词词组去命名. 类型定义了属性和行为.虽然它包含行为,但不是行为本身.所 ...

  9. 学习python2

    字符串遍历 列表遍历 元组遍历 字典遍历 <1> 遍历字典的key(键) <2> 遍历字典的value(值) <3> 遍历字典的项(元素) <4> 遍历 ...

  10. 解决word2013老是打开未响应情况

    问题:自己装了word2013时,每次打开word或者工作时,老是出现一个圈圈,提示未反应,是否关闭程序这样的提示: 解决方法:文件->选项->高级->显示->禁用硬件加速,将 ...