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. GUI的最终选择 Tkinter(八):Message组件、Spinbox组件、PanedWindow组件、Toplevel组件

    Message组件 Message(消息)组件是Label组件的变体,用于显示多行文本消息,Message组件能够自动执行,并调整文本的尺寸使其适应给定的尺寸. from tkinter import ...

  2. CellSet 遍历

    CellSet 结构: 查询MDX: SELECT NON EMPTY {{ {{ {{ {{ {{ AddCalculatedMembers([店铺.店铺ID].[店铺ID].Members)}} ...

  3. SSIS控件使用

    1.转换控件: 2.执行SQL任务,返回一个值后,判断全量,还是增量?

  4. 洛谷P3928 SAC E#1 - 一道简单题 Sequence2

    提交地址 题目背景 小强和阿米巴是好朋友. 题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强. ...

  5. 【转】说说Runnable与Callable

    说说Runnable与Callable   Callable接口:   Runnable接口: 相同点: 两者都是接口:(废话) 两者都可用来编写多线程程序: 两者都需要调用Thread.start( ...

  6. feign实现服务间的负载均衡

    feign Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单.我们只需要使用Feign来创建一个接口并用注解来配置它既可完成.它具备可插拔的注解 ...

  7. C++程序中调用WebService的实现

    前言 因为最近的项目中需要运用到在MFC程序中调用WebService里面集成好了的函数,所以特意花了一天的时间来研究WebService的构建以及如何在MFC的程序中添加Web引用,进而来实现在C+ ...

  8. vuejs 学习旅程一

    来上海快一年了,一直在东钿金融工作着,这一年来主要负责公司前期的房产评估微信平台,公司IT部也是刚刚成立,成立IT部门不仅仅只是维护房产评估微信,而是要做一个互金理财平台.于是我一年来的主要工作是负责 ...

  9. tomcat配置https 和 http强制跳转https

    https是http+ssl的可进行加密传输,身份认证的网络协议,防止数据在传输过程中被窃取.因此,https将得到越来越广泛的应用,下面是如何配置tomcat服务器让http自动转到https的步骤 ...

  10. telegraf1.8+influxdb1.6+grafana5.2 环境搭建 结合JMeter3.2

    telegraf1.8+influxdb1.6+grafana5.2 环境搭建 结合JMeter3.2 参考地址:https://blog.csdn.net/laisinanvictor/articl ...