随笔分类 -  BFS

摘要:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest l 阅读全文
posted @ 2017-12-03 06:35 apanda009 阅读(125) 评论(0) 推荐(0)
摘要:Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- w 阅读全文
posted @ 2017-11-30 08:35 apanda009 阅读(160) 评论(0) 推荐(0)
摘要:Taken from GeeksforGeeks Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth First Search (BFS) :- Al 阅读全文
posted @ 2017-11-08 03:14 apanda009 阅读(196) 评论(0) 推荐(0)
摘要:题目给了我们两个数组,一个是进程的数组,还有一个是进程数组中的每个进程的父进程组成的数组。题目中说结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以我们首先要理清父子进程的关系。所以我们使用一个哈希表,建立进程和其所有子进程之间的映射,然后我们首先把要结束的进程放入一个队 阅读全文
posted @ 2017-10-10 00:23 apanda009 阅读(225) 评论(0) 推荐(0)
摘要:类似200. Number of Islands, 但是lc考察的是题意的转化, 而不是光会个框架 考察: 对邻居的理解, 什么才是真正的邻居, 对邻居的遍历 UF 面试常考 阅读全文
posted @ 2017-08-08 09:19 apanda009 阅读(133) 评论(0) 推荐(0)
摘要:在Binary Tree Level Order Traversal中我们是维护了一个队列来完成遍历,而在这里为了使每次都倒序出来,我们很容易想到用栈的结构来完成这个操作。有一个区别是这里我们需要一层一层的来处理(原来可以按队列插入就可以,因为后进来的元素不会先处理),所以会同时维护新旧两个栈,一个 阅读全文
posted @ 2017-08-07 00:07 apanda009 阅读(179) 评论(0) 推荐(0)
摘要:现在开始考bfs 了吗, 根据size办事,分情况了 dfs: 先序遍历 + 树的深度, 跟此题类似: 199 Binary Tree Right Side View 阅读全文
posted @ 2017-08-06 18:08 apanda009 阅读(165) 评论(0) 推荐(0)
摘要:经典bfs, 不多说 阅读全文
posted @ 2017-08-06 17:39 apanda009 阅读(148) 评论(0) 推荐(0)
摘要:BFS 1. matrix 邻居都是找好的, 但是此题是八邻居问题 2. visited 数组是防止邻居重叠遍历的问题和重复加入的问题, 而此题需要遍历所有的邻居 但是此题再往队列加的时候判断是否为'E', 并且将其转化为'B' 3. and all of its adjacent unreveal 阅读全文
posted @ 2017-08-05 10:22 apanda009 阅读(233) 评论(0) 推荐(0)
摘要:这题考点在于需要设置两个visited数组 dfs 与bfs不同之处在于使调用符合题意的自己, 而bfs是遍历符合条件的兄弟, 用Queue BFS 当 if(pacific[i][j] && atlantic[i][j]) 时才满足题意 阅读全文
posted @ 2017-08-04 22:33 apanda009 阅读(136) 评论(0) 推荐(0)
摘要:矩阵的bfs, 考察: 1.哪些是先入队的? 外围? 还是遍历所有符合条件的? 此处改成Integer.Max_Value, 2.入队之后, 邻居元素怎么改让其入队. 怎么跳过不符合题意的元素, visited[] 是否使用 3一般用在改矩阵的题中 tips, 可以该元素e.g. 将'o' 改成'$ 阅读全文
posted @ 2017-08-02 11:45 apanda009 阅读(145) 评论(0) 推荐(0)
摘要:容器此处还可以用 hashMap, 其中set既可以表示里面的边又可以表示入度数, 但是此处只需要知道入度数就可以了. 之前还有用 int [] ArrayList[] 来表示的同set, 因为坐标可以表示map中的键, 容器内元素可以表示值. 阅读全文
posted @ 2017-07-23 21:36 apanda009 阅读(160) 评论(0) 推荐(0)
摘要:矩阵的bfs, 套路一致,从外向内, 很easy 构造类, 遍历矩阵建立图 bfs要点在于如何建图, 是否建类, 建比较器, 建方向容器, 建走过的路的存储器(数组, 或者set, list) 如何遍历(堆不空?), 遍历到内部的点时判断是否符合题意(边界, 走过), 再考虑题意, 判断当前的点是否 阅读全文
posted @ 2017-07-23 21:06 apanda009 阅读(172) 评论(0) 推荐(0)
摘要:思路 先修课程是拓扑排序的经典应用, 这里相当于找有向图是否有环, 如果有环的话拓扑排序能遍历到的节点将少于图的节点. 这里我们建立一个图, 用一个数组记录每个节点的入度. 对图进行拓扑排序 复杂度 时间O(V+E) 空间 O(V) 有向图: 入度和边, 用什么容器, 怎么生成图, 根据什么入队, 阅读全文
posted @ 2017-07-23 20:16 apanda009 阅读(234) 评论(0) 推荐(0)
摘要:Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to 阅读全文
posted @ 2017-07-13 22:22 apanda009 阅读(163) 评论(0) 推荐(0)