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轴的线 ...
随机推荐
- python大战机器学习——数据降维
注:因为公式敲起来太麻烦,因此本文中的公式没有呈现出来,想要知道具体的计算公式,请参考原书中内容 降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中 1.主成分分析(PCA) 将n ...
- css中如何设置透明度
怎样在CSS样式中设置背景的透明度,下面一个具体的实例.把类为box的层设为透明.<div class="box"></div><style>. ...
- ASP.NET Core性能测试
ASP.NET Core性能测试 应用性能直接影响到托管服务的成本,因此公司在开发应用时需要格外注意应用所使用的Web框架,初创公司尤其如此.此外,糟糕的应用性能也会影响到用户体验,甚至会因此受到相关 ...
- docker 在Windows下使用遇到的坑
1.大部分系统不支持直接安装docker for windows,只能使用docker toolbox,相当于在Windows上安装了一个linux的虚拟机 2.启动docker toolbox的时候 ...
- 软件模拟I2C时输入与输出切换
一 为达到类似C51的操作需要添加以下位带操作:#include "stm32f10x_gpio.h"#include "stm32f10x_conf.h" / ...
- 通过sqlserver sa密码修改windows操作系统密码
如果你不记得windows管理员的密码了,但知道sqlserver sa用户的密码,可以通过以下方式修改: 进入SQL之后执行以下语句: -- 允许配置高级选项 EXEC sp_configure ...
- 洛谷P1965 转圈游戏
https://www.luogu.org/problem/show?pid=1965 快速幂 #include<iostream> #include<cstdio> #inc ...
- P4876 近似排列计数50
时间限制:1s 内存限制:256MB [问题描述] 对于一个1-n的排列,如果满足第i个数|ai-i|<=k,则称该排列为K-近似排列. 现在排列的若干位置已经确定,你需要计算剩下的数有多少种排 ...
- 在Unity3d中解析Lua脚本的方法
由于近期项目中提出了热更新的需求,因此本周末在Lua的陪伴下度过.对Lua与Unity3d的搭配使用,仅仅达到了一个初窥门径的程度,记录一二于此.水平有限,欢迎批评指正. 网络上关于Lua脚本和Uni ...
- 【持续更新】MyBatis相关
MyBatis开发结构 #与$的区别