DFS算法:

explore(G, v)
visited(v) = true
previsit(v)
for each edge(v, u) in E:
if not visited(u): explore(u)
postvisit(v)
dfs(G)
for all v in V:
visited(v) = false
for all v in V:
if not visited(v): explore(v)

应用:
1) 判断顶点u与v之间是否存在路径
2) 判断一个无向图是否连通


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.

解题思路:

深度优先。使用递归的方式写。直接贴代码,简单易懂。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL)
return false;
if (root->val == sum && root->left == NULL && root->right == NULL)
return true;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};

类似的一道题:

129. Sum Root to Leaf Numbers

解题思路:走到叶子节点的时候加上cur值。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root) {
if (root == NULL)
return 0;
sum = 0;
dfs(root, 0);
return sum;
}
void dfs(TreeNode* root, int curSum) {
int cur = curSum * 10 + root->val;
if (root->left == NULL && root->right == NULL)
sum += cur;
if (root->left != NULL)
dfs(root->left, cur);
if (root->right != NULL)
dfs(root->right, cur);
}
private:
int sum;
};

类似的题:

257. Binary Tree Paths

解题思路:

在叶子节点时将nums转换为string放入path中。注意,因为每个节点的值都压栈了,所以每个压入nums的值,最后都要出栈,不然打印路径时会有重复。

数字转字符串:sprintf

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
if (root == NULL)
return path;
vector<int> nums;
dfs(root,nums);
return path;
}
void dfs(TreeNode* root, vector<int> nums) {
if (root->left == NULL && root->right == NULL) {
nums.push_back(root->val);
string str="";
for (int i = 0; i < nums.size(); i++) {
if (i != 0)
str += "->";
char arr[10];
sprintf(arr, "%d", nums[i]);
str += arr;
}
path.push_back(str);
nums.pop_back();
}
if (root->left != NULL) {
nums.push_back(root->val);
dfs(root->left, nums);
nums.pop_back();
}
if (root->right != NULL) {
nums.push_back(root->val);
dfs(root->right, nums);
nums.pop_back();
}
}
private:
vector<string> path;
};

    


拓扑排序:

1) Find a source, output it, and delete it from the graph. Repeat until the graph is empty.

2) dfs and sort with post[u] in descending order: TOPOLOGICAL-SORT(G)

1 call DFS(G) to compute finishing times post[v] for each vertex v

2 as each vertex is finished, insert it onto the front of a linked list

3 return the linked list of vertices

leetcode-11-dfs的更多相关文章

  1. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  2. 【LeetCode】DFS 总结

    DFS(深度优先搜索) 常用来解决可达性的问题. 两个要点: 栈:用栈来保存当前节点信息,当遍历新节点返回时能够继续遍历当前节点.可以使用递归栈. 标记:和 BFS 一样同样需要对已经遍历过的节点进行 ...

  3. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  4. LeetCode 11 水池蓄水问题

    今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...

  5. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  6. leetcode 39 dfs leetcode 40 dfs

    leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...

  7. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  8. LeetCode 11

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  9. LeetCode——11. Container With Most Water

    一.题目链接:https://leetcode.com/problems/container-with-most-water/ 二.题目大意: 给定n个非负整数a1,a2....an:其中每一个整数对 ...

  10. LeetCode 11 Container With Most Water(分支​判断问题)

    题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description   Problem: 已知n条垂直于x轴的线 ...

随机推荐

  1. Python 开发基础-字符串类型讲解(字符串方法)-2

    s = 'Hello World!'print(s.index('W',0,9))#返回某个字母的索引值,本例返回6.没有该字母会报错,和FIND比较像,find不会报错,没找到会返回-1print( ...

  2. struts2 具体学习资料

    [struts2]<package>的配置:https://www.cnblogs.com/ningvsban/p/3734562.html struts2  具体学习资料 :http:/ ...

  3. 关系型数据库---MySQL---数据类型

    一.每个数据表至少有一个数据列.用户必须为每一个数据列分别定义一个适当的数据类型: 1.整数(***Int) 1.1 默认情况下,整数类型包括:正整数.负整数: 1.2 如果给数据列定义了unsign ...

  4. C - AtCoDeerくんと選挙速報 / AtCoDeer and Election Report

    ceil有毒啊..用ceil一直错. 思路就是模拟吧,设当前的答案是ansx和ansy. 如果比例是小于ansx的,那么就要乘以一个倍数k1,使得a * k1 >= ansx的. 所以就用cei ...

  5. HDU 5875 H - Function 用单调栈水过了

    http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...

  6. C#字符串变量使用

    string由于是引用类型,所以,声明的字符串变量会存储到堆上,而且该变量是不可变的,一旦初始化了该变量,该内存区域中存储的内容将不能更改.在对字符串操作时,是在堆上创建了一个新的字符串变量,并将新的 ...

  7. 给Visual Studio更换皮肤和背景图

    1.先安装更换皮肤的插件 VS菜单栏里面找到:工具>扩展和更新>联机>搜索: Theme Editor 下载并安装: 安装后先不着急重启VS 然后,安装可更改背景图片的插件:工具&g ...

  8. AJPFX深入理解之abstract class和interface的区别

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...

  9. NIO学习之Channel

    一.Channel基础 通道是一个对象,通过它可以读取和写入数据,Channel就是通向什么的道路,为数据的流向提供渠道: 在传统IO中,我们要读取一个文件中的内容使用Inputstream,该str ...

  10. To run dex in process, the Gradle daemon needs a larger heap

    http://blog.csdn.net/u012995856/article/details/52595653