LeetCode Path Sum II (DFS)
题意:
给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回。
思路:
节点的值可能为负的。这样子就必须到了叶节点才能判断,而不能中途进行剪枝。
/**
* 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<vector<int> > pathSum(TreeNode* root, int sum) {
if(root==NULL) return ans;
DFS(root,,sum,tmp);
return ans;
}
void DFS(TreeNode* t,int n,const int& sum,vector<int>& num)
{
num.push_back(t->val);
n+=t->val; if(t->left==NULL&&t->right==NULL)
{
if(n==sum) ans.push_back(num);
}
else
{
if(t->left) DFS(t->left,n,sum,num);
if(t->right) DFS(t->right,n,sum,num);
}
num.pop_back();
}
private:
vector<vector<int>> ans;
vector<int> tmp;
};
AC代码
LeetCode Path Sum II (DFS)的更多相关文章
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- LeetCode Combination Sum III (DFS)
题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...
- 【树】Path Sum II(递归)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)
Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)
Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...
- Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)
Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...
随机推荐
- 实战SQL Server 2005镜像配置全过程
SQL Server 2005镜像配置基本概念 我理解的SQL Server 2005镜像配置实际上就是由三个服务器(也可以是同一服务器的三个 SQL 实例)组成的一个保证数据的环境,分别是:主服务器 ...
- 键盘控制select选项上下
$('#k').live('keydown',function(event){ if (event.keyCode==38){ /*$(this).addClass("active" ...
- iOS 登陆的实现四种方式
iOS 登陆的实现四种方式 一. 网页加载: http://www.cnblogs.com/tekkaman/archive/2013/02/21/2920218.ht ml [iOS登陆的实现] A ...
- NOIP2005 等价表达式 解题报告
明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数表达式是和 ...
- zabbix3.0部署(LAMP)
0.1 初始化 #!/bin/sh yum clean all systemctl stop firewalld.service systemctl disable firewalld.service ...
- [开发笔记]-火狐的event和jquery1.9.1.min的问题
一:火狐不兼容window.event.keyCode问题 火狐的event是以参数形式传入的 function onlychinese(event) { event = event || windo ...
- RM报表的打印偏移
自己摸索一下 RMReport1.SaveReportOptions.AutoLoadSaveSetting := True; RMReport1.SaveReportOptions.UseRegis ...
- C#根据当前日期获取星期和阴历日期
private string GetWeek(int dayOfWeek) { string returnWeek = ""; switch (dayOfWeek) { case ...
- HDU 1465
排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 大家常常 ...
- xcode 5.0 以上去掉icon高亮方法&iOS5白图标问题
之前的建议方法是把在xxx.info.plist文件中把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,反复实现不成功,今天 ...