Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
]

题目大意:给一个二叉树和一个数,求从根节点到叶结点的和等于这个数的所有路径。

解题思路:DFS,首先把当前节点入栈,然后分别递归左右子树,如果当前节点为空直接退出,如果当前节点是叶结点但是到根节点的路径和不等于指定的数也直接退出,如果等于指定的数,那么把这条路径加入结果List,递归完之后回退到上一层。

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
List<Integer> tmp = new ArrayList<>();
path(root, tmp, res, sum);
return res;
} public void path(TreeNode node, List<Integer> tmp, List<List<Integer>> res, int sum) {
if (node == null) {
return;
}
if (sum != node.val && node.left == null && node.right == null) {
return;
}
tmp.add(node.val);
if (sum == node.val && node.left == null && node.right == null) {
res.add(new ArrayList<>(tmp));
tmp.remove(tmp.size() - 1);
return;
}
path(node.left, tmp, res, sum - node.val);
path(node.right, tmp, res, sum - node.val);
tmp.remove(tmp.size() - 1);
}

Path Sum II——LeetCode的更多相关文章

  1. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  2. Path Sum II leetcode java

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  5. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  6. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  7. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  8. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  9. 【leetcode】Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

随机推荐

  1. C++:类成员函数的重载、覆盖和隐藏区别?

    #include <iostream> class A { public: void func() { std::cout << "Hello" <& ...

  2. Java基础知识强化之集合框架笔记06:Collection集合存储自定义对象并遍历的案例

    1.练习:用集合存储5个学生对象,并把学生对象进行遍历. 分析: (1)创建学生类(2)创建集合对象(3)创建学生对象(4)把学生添加到集合(5)把集合转成数组(6)遍历数组 2. 代码示例: Stu ...

  3. 模拟电路"虚短" & "虚断"

    <虚短 & 虚断> 运算放大器组成的电路五花八门,令人眼花瞭乱,是模拟电路中学习的重点.遍观所有模拟电子技朮的书籍和课程,在介绍运算放大器电路的时候,无非是先给电路来个定性,比如这 ...

  4. Ouath协议

    OAuth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某一网站.移动或桌面应用上存储的私密的资源(如用户个人信息.照片.视频.联系人列表),而无需将用户名和密码提供给第三方应用. ...

  5. Wpf Binding.Path设置

    Binding.Path 获取或设置绑定源属性的路径. 每个绑定通常都具有四个组件:绑定目标对象.目标属性.绑定源,以及要使用的绑定源值的路径.有关这些数据绑定概念的更多信息,请参见数据绑定概述. 使 ...

  6. 【USACO 1.3.3】回文串

    [題目描述] 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文.你的工作就是去寻找这些牛制造的奇观(最棒的回文). 在寻找回文时不用理睬那些标点符号.空 ...

  7. Linux导航神器-----autojump

    对于命令行用户来说,频繁的cd和tab应该是日常工作中最多使用的命令了.特别对于重度用户来说,如果可以省去这么多cd和tab,将更多的时间做有意义的事该多好.其实Linux的学习过程本身就行这样.你会 ...

  8. Spring4.0学习笔记(10) —— Spring AOP

    个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...

  9. Rabbitmq init terminating in do boot 问题

    其实出现这个问题的原因很简单,就是rabbitmq的安装目录中是不能带空格的,但是官方安装包会默认的将我们的程序安装到Program Files下,哭晕啊有没有.所以,我们将rabbitmq从D:\P ...

  10. javascript 16/1/14随记

    1.想在一个事件或者函数之后,触发某个事件. var flag=false //定义一个全局变量 function aku(){ if(flag){ } } sizemousedown=functio ...