
dfs
文章平均质量分 72
liuchenjane
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
根据两种不同遍历结果重构二叉树
leetcode原题:题目1 题目2 这里假设给的遍历都有中序遍历,我们先根据先序遍历或后序遍历找到根结点的值,然后据此可判断,在中序遍历的结果向量中,在根节点之前的部分数组组成它的左子树,根节点之后组成它的右子数。采用深度递归dfs。题1:已知中序和后序遍历结果/** * Definition for a binary tree node. * struct TreeNode { *转载 2016-10-08 10:42:36 · 500 阅读 · 0 评论 -
93. Restore IP Addresses
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.13转载 2016-10-25 20:53:39 · 303 阅读 · 0 评论 -
200. Number of Islands
200. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally转载 2016-10-26 15:54:42 · 302 阅读 · 0 评论 -
图的深度优先搜索DFS(C++实现)
//深度优先搜索算法在搜素过程中对节点进行涂色来指明节点的状态。 //每个节点的初始颜色都是白色的,在节点被发现后变成灰色的,在其邻接链表被扫描完成后变为黑色的。 //该方法保证每个节点仅在一颗深度优先树中出现,因此,所有的深度优先树都是不相交的。 //深度优先搜素的另一个性质:节点的发现时间和完成时间具有所谓的括号化的结构 #include #include #include #原创 2016-11-08 20:20:41 · 10171 阅读 · 2 评论 -
放苹果——华为机试题
题目描述 题目描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。 输入 每个用例包含二个整数M和N。0 样例输入 7 3 样例输出 8 /** * 计算放苹果方法数目 * 输入值非法时返回原创 2016-11-15 21:34:40 · 492 阅读 · 0 评论 -
207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a p转载 2016-12-24 13:45:12 · 331 阅读 · 0 评论 -
473. Matchsticks to Square
473. Matchsticks to Square Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all转载 2016-12-24 15:54:44 · 571 阅读 · 0 评论 -
508. Most Frequent Subtree Sum
Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (includi原创 2017-02-09 17:05:37 · 434 阅读 · 0 评论 -
491. Increasing Subsequences
Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 . Example: In原创 2017-02-11 22:06:40 · 361 阅读 · 0 评论 -
494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out ho原创 2017-01-25 12:51:56 · 707 阅读 · 0 评论 -
529. Minesweeper
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty squar原创 2017-02-26 14:51:00 · 958 阅读 · 2 评论 -
133. Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each n转载 2016-10-14 19:08:41 · 251 阅读 · 0 评论 -
130. Surrounded Regions
130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounde转载 2016-11-04 09:18:31 · 347 阅读 · 0 评论 -
Subsets II
Subsets II Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums转载 2016-09-30 17:01:23 · 316 阅读 · 0 评论 -
Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24原创 2016-09-25 19:51:19 · 379 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 分析:将一个有序数组转化成平衡的BST,找到中值,作为根节点,再递归分别生成左子树与右子数原创 2016-09-23 16:26:43 · 285 阅读 · 0 评论 -
78. Subsets
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is:原创 2016-09-23 10:17:26 · 205 阅读 · 0 评论 -
Permutations I , II
Permutations Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1],转载 2016-09-20 10:17:42 · 402 阅读 · 0 评论 -
401. Binary Watch
401. Binary Watch A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents a zero or one, with the转载 2016-09-18 18:54:51 · 565 阅读 · 0 评论 -
Combination sum I, II,III && Combination
leetcode上遇到了combination sum,combination,permutation;这些排列组合都与回溯法有关, 而且都用递归。 1. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w转载 2016-09-17 18:20:37 · 374 阅读 · 0 评论 -
199. Binary Tree Right Side View
199. Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Gi原创 2016-10-21 16:28:01 · 293 阅读 · 0 评论 -
Path Sum I,II
112. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below b转载 2016-10-12 08:56:21 · 308 阅读 · 0 评论 -
437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the原创 2016-10-23 17:27:33 · 1119 阅读 · 0 评论 -
536. Construct Binary Tree from String
You need to construct a binary tree from a string consisting of parenthesis and integers.The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthes原创 2017-03-12 14:29:23 · 655 阅读 · 0 评论