leetcode-11-dfs
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的更多相关文章
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【LeetCode】DFS 总结
DFS(深度优先搜索) 常用来解决可达性的问题. 两个要点: 栈:用栈来保存当前节点信息,当遍历新节点返回时能够继续遍历当前节点.可以使用递归栈. 标记:和 BFS 一样同样需要对已经遍历过的节点进行 ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode 11 水池蓄水问题
今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- leetcode 39 dfs leetcode 40 dfs
leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 11
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode——11. Container With Most Water
一.题目链接:https://leetcode.com/problems/container-with-most-water/ 二.题目大意: 给定n个非负整数a1,a2....an:其中每一个整数对 ...
- LeetCode 11 Container With Most Water(分支判断问题)
题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description Problem: 已知n条垂直于x轴的线 ...
随机推荐
- Hive进阶_Hive数据的导入
使用Load语句执行数据的导入 语法: load data [local] inpath 'filepath' [overwrite] into table tablename [partition ...
- 泛型设计<T, PK extends Serializable>
泛型类和泛型方法的使用 http://www.jb51.net/article/67445.htm http://www.cnblogs.com/iyangyuan/archive/2013/04/0 ...
- Spark Mllib里如何建立向量标签(图文详解)
不多说,直接上干货! 注意: val pos = LabeledPoint(1, vd) val neg = LabeledPoint(2, vs) 除了这两种建立向量标签.还可以从数据库中获取固定格 ...
- java分为 三类 ME,SE,EE
java分为 三类 ME,SE,EE Java SE=Java Standard EditionJava EE=Java Enterprise Edition Java ME=Java Mobile ...
- MySQL 实现字符串换行
target_describe字段值中包含 :[ 这两个特殊的字符 ,想要在字符之间加换行 需要插入CHAR(10) ),'[')) UPDATE ew_pm_project_red_detail S ...
- ef 操作 mysql 中文乱码问题
1.保证mysql数据的编码为utf8 启动mysql mysql -hlocalhost -uroot -p 输入密码 show VARIABLES like 'character_%'; SET ...
- 天上掉馅饼 期望DP
C 天上掉馅饼文件名 输入文件 输出文件 时间限制 空间限制bonus.pas/c/cpp bonus.in bonus.out 1s 128MB题目描述小 G 进入了一个神奇的世界,在这个世界,天上 ...
- App配置页面头部
记录一下 App配置页面头部 例 上图红色框部分就是自己配置的头部 //我的客户 "/OACustomer/Index": { title: "我的客户", h ...
- 在前端解決顯示null值一例
用.net core2.1 api返回的JSON里包含null值,前端直接顯示出來了,影響閱讀, 用以下語句將null轉換為空字符串: // Build html. html += "< ...
- Flat UI theme--扁平化的UI
项目地址:点击打开 支持版本: jQuery Mobile 1.3.2 使用很简单,前提是你的前端是在jquery-mobile的基础上开发的,然后导入相应的css文件.img文件和js文件即可. 案 ...