
BFS / DFS
yuccess
这个作者很懒,什么都没留下…
展开
-
深度优先遍历与广度优先遍历
深度优先遍历 1.深度优先遍历的递归定义 假设给定图G的初态是所有顶点均未曾访问过。在G中任选一顶点v为初始出发点(源点),则深度优先遍历可定义如下:首先访问出发点v,并将其标记为已访问过;然后依次从v出发搜索v的每个邻接点w。若w未曾访问过,则以w为新的出发点继续进行深度优先遍历,直至图中所有和源点v有路径相通的顶点(亦称为从源点可达的顶点)均已被访问为止。若原创 2016-10-27 16:17:46 · 343 阅读 · 0 评论 -
79, Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically原创 2017-01-02 01:26:44 · 218 阅读 · 0 评论 -
93. Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order原创 2017-01-01 21:11:11 · 298 阅读 · 0 评论 -
117. Populating Next Right Pointers in Each Node II 非常精炼的代码,值得好好体会
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use原创 2016-12-09 22:41:27 · 210 阅读 · 0 评论 -
110. Balanced Binary Tree 判定是否平衡
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node neve原创 2016-12-09 15:19:57 · 497 阅读 · 0 评论 -
深度优先,广度优先的学习(待续)
1,例子: 求树的最大深度?原创 2016-12-06 23:06:29 · 355 阅读 · 0 评论 -
109. Convert Sorted List to Binary Search Tree
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 解法: /** * Definition for singly-linked list. * struct ListNode { * int val原创 2016-12-13 00:58:01 · 328 阅读 · 0 评论 -
129. Sum Root to Leaf Numbers 注意要和124题对比,都是DFS问题
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find原创 2016-12-11 22:25:09 · 223 阅读 · 0 评论 -
124. Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connecti原创 2016-12-11 21:39:34 · 270 阅读 · 0 评论 -
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]原创 2016-12-31 23:47:13 · 219 阅读 · 0 评论