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. awk单引号处理

    awk中使用单引号,常规字符串,'\''即可,但如果像下面在$4变量用单引号,则还需要加上双引号才行. cat 2.txt | awk '{ print $1, $2, $3, "'\''& ...

  2. 开发工具~nuget配置本地源

    我们在本地部署了自己的nuget服务器,有时可以需要用到nuget restore命令去恢复包包,它会从下面的nuget.config里读你的配置源信息,就是在这里,我们要把内测的nuget服务器路径 ...

  3. vuex填坑记录

    vuex是需要等待页面加载完成后才会更新的,如果页面点击刷新有用到vuex的地方,那么vuex会保持旧的数据,等刷新完成后vuex的数据才会重置,所以要在页面加载后再调用vuex的数据才是正确的,如果 ...

  4. c#基础-构造函数 this new

    构造函数作用:帮助我们初始化对象(给对象的每个属性依次的赋值)构造函数是一个特殊的方法:1).构造函数没有返回值,连void也不能写.2).构造函数的名称必须跟类名一样. 创建对象的时候会执行构造函数 ...

  5. display:none和visibility:hidden v-show和v-if的区别

    隐藏元素display:none 和 visibility:hidden的区别visibility:hidden可以隐藏某个元素,但是隐藏的元素仍要占据空间,仍要影响布局display:none不会占 ...

  6. 使用 xib 设置 button 等款等高

    很多时候需要使用平分的控件来布局,当然xib中可以之间使用 UIToolBar 使用 UIBarButtonItem 添加弹簧即可完成平均分布 但是,直接使用 button 也可以实现平均布局

  7. html-jquery/js引用外部图片时遇到看不了或出现403情况解决方法

    <script type="text/javascript"> function showImg(url) { var frameid = 'frameimg' + M ...

  8. layout_weight属性

    layout_weight 某个控件text多行,第一行对齐,baselineAligned = "false" 可使控件对齐 weight 计算规则 剩余尺寸=父布局尺寸-子布局 ...

  9. JFinal免费公开课更新中

    价值千元的课程,免费报名学习,JFinal学院-小木 录制JFinal视频教程,JFinal核心已经周边涉及到微信小程序开发.数据库.前端实战等.

  10. Nginx和Apache服务器上配置反向代理

    在实际项目过程中,由于网站要用到一个在线编辑器(个性化的在线编辑软件),需要跨域进行通信!由于跨域通信较多,所以当时就想到在网站服务器上代理编辑软件的请求! 这就是“反向代理”的实际需求! 一.Ngi ...