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]
]
 class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<Integer> curres = new ArrayList<Integer>();
help(root,0,sum,curres);
return res;
}
private void help(TreeNode root,int sum ,int target,List<Integer> curres){
if(root == null) return ;
curres.add(root.val);
if(root.left==null && root.right==null && sum+root.val==target)
res.add(new ArrayList(curres));
help(root.left,sum+root.val,target,curres);
help(root.right,sum+root.val,target,curres);
curres.remove(curres.size()-1);
}
}

113. Path Sum II(求等于某个数的所有路径)的更多相关文章

  1. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

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

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

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

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

  4. 【LeetCode】113. 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 ...

  5. [LeetCode] 113. Path Sum II 二叉树路径之和之二

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

  6. 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 ...

  7. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 113. Path Sum II (Tree; DFS)

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

  9. leetcode 113. Path Sum II (路径和) 解题思路和方法

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

  10. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

随机推荐

  1. Flask 安装 快速入门

    $ pip install flask Flask自带的Server在端口5000上监听: ython app.py flask通过request.form['name']来获取表单的内容. 外部可见 ...

  2. 拖拽 支持ie6

    可随意拖拽方块至任一位置: 1.setCapture方法:多用于容器对象,效果是对指定的对象设置鼠标捕获.使在容器内的子对象的鼠标事件均由容器对象触发,因此,只能在容器对象的鼠标事件函数中进行处理.当 ...

  3. JSP JSP(Java Server Page)是一种实现普通静态HTML和动态页面输出混合编码的技术

    JSP JSP(Java Server Page)是一种实现普通静态HTML和动态页面输出混合编码的技术.从这一点来看,非常类似Microsoft ASP.PHP等技术.借助形式上的内容和外观表现的分 ...

  4. Android开源-NineOldAndroids

    开源地址: https://github.com/JakeWharton/NineOldAndroids 简单介绍:NineOldAndroids是一款支持在低版本号开发的Android动画的框架 包 ...

  5. C++设计模式之建造者模式(二)

    3.省略指挥者Director的建造者模式 指挥者类Director在建造者模式中扮演很关键的数据.简单的Director类用于指导详细建造者怎样构建产品,它按一定次序调用Builder的buildP ...

  6. Linux系统常用工具集

    整理Linux系统下一些日常工作中常用工具,旨在提高效率: 1.截图软件Shutter 2.通讯聊天工具pidgin 3.守护进程工具daemontools 4.远程桌面服务TigerVNC 5.Ma ...

  7. 【SR】MAP

    MAP:最大后验概率(Maximum a posteriori) 估计方法根据经验数据获得对难以观察的量的点估计.它与最大似然估计中的 Fisher方法有密切关系, 但是它使用了一个增大的优化目标,这 ...

  8. hdu 4067(最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4067 思路:很神奇的建图,参考大牛的: 如果人为添加t->s的边,那么图中所有顶点要满足的条件都 ...

  9. mysql core文件的正确打开姿势

         最近两天自己负责的一个实例频繁出现crash的情况,分析了日志,大致明白了crash的原因,但是没有定位到具体的SQL,也没有找到很好的规避的办法,因此想在mysql出现crash的时候自动 ...

  10. [Algorithms] Counting Sort

    Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...