BFS
文章平均质量分 64
再见小小ronnie
这个作者很懒,什么都没留下…
展开
-
Leetcode 127. Word Ladder
Think those words can form a graph, do the BFS in the graph to find the shortest path from the startWord to the endWord.public class Solution { public int ladderLength(String beginWord, String e原创 2017-02-07 12:10:04 · 248 阅读 · 0 评论 -
Leetcode 297. Serialize and Deserialize Binary Tree
public class Codec { private static final String NULL = "null"; private static final String SPLT = " "; // BFS saves nodes level by level public String serialize(TreeNode root) {原创 2017-03-25 08:11:20 · 206 阅读 · 0 评论 -
Leetcode 449. Serialize and Deserialize BST
public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { // If tree is empty just return a null string if (root == null)原创 2017-03-29 02:22:13 · 669 阅读 · 0 评论 -
Leetcode 210. Course Schedule II
public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int[] ret = new int[numCourses]; HashMap> graph = buildGraph(prerequisites);原创 2017-02-09 11:39:16 · 173 阅读 · 0 评论 -
Leetcode 207. Course Schedule
/** * Build the graph using the list of edges. * The idea is to check is the graph has circle. * Start from nodes which indegree are zero and traverse the whole graph using BFS. * A variable coun...原创 2017-02-09 10:40:25 · 178 阅读 · 0 评论 -
Leetcode 130. Surrounded Regions
/** * Starting from four borders, if we meet an 'O' do the BFS on this point. * We need to check if the coordiantes of its neighbors are valid (i.e. inside the board) and all the value of neighbors原创 2017-02-09 03:08:37 · 179 阅读 · 0 评论 -
Route Between Two Nodes in Graph
public class Solution { public boolean hasRoute(ArrayList graph, DirectedGraphNode s, DirectedGraphNode t) { if (s == t) { return true; }原创 2017-02-08 06:12:25 · 237 阅读 · 0 评论 -
Leetcode Graph Valid Tree
public class Solution { public boolean validTree(int n, int[][] edges) { if (n-1 != edges.length) { return false; } // construct the undirected tree us原创 2017-02-08 05:47:55 · 475 阅读 · 0 评论 -
Leetcode 133. Clone Graph
/** * Steps to clone a graph. * i. clone all nodes in the graph * ii. clone all neighbors for the new nodes * we need a hashmap to save the mapping of old and new nodes, * then we can copy all ne原创 2017-02-08 04:18:03 · 136 阅读 · 0 评论 -
Topological Sorting
/** * Definition for Directed graph. * class DirectedGraphNode { * int label; * ArrayList neighbors; * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList(); } * }; */p原创 2017-02-08 03:16:42 · 210 阅读 · 0 评论 -
Leetcode Binary Tree Level Order Traversal II
public class Solution { public List> levelOrderBottom(TreeNode root) { List> ret = new ArrayList<>(); if (root == null) return ret; Queue queue = new LinkedList<>(原创 2017-02-07 12:20:06 · 248 阅读 · 0 评论 -
Leetcode 103. Binary Tree Zigzag Level Order Traversal
Similar with Binary Tree Level Order Traverse. The difference is reverse the List when it is necessary. public class Solution { public List> zigzagLevelOrder(TreeNode root) { List> ret =原创 2017-02-07 12:18:12 · 137 阅读 · 0 评论 -
Leetcode 102. Binary Tree Level Order Traversal
Typical BFS probelm, note that we need to save the size of the current queue and then use a for loop to save the result level by level. public class Solution { public List<List<Integer...原创 2017-02-07 12:14:43 · 143 阅读 · 0 评论 -
Leetcode 257. Binary Tree Paths
DFSclass Solution: """ @param root: the root of the binary tree @return: all root-to-leaf paths """ def binaryTreePaths(self, root): # write your code here if no...原创 2018-09-13 07:35:26 · 132 阅读 · 0 评论