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,

  1. 5
  2. / \
  3. 4 8
  4. / / \
  5. 11 13 4
  6. / \ / \
  7. 7 2 5 1

return

  1. [
  2. [5,4,11,2],
  3. [5,8,4,5]
  4. ]
  1. class Solution {
  2. List<List<Integer>> res = new ArrayList<>();
  3. public List<List<Integer>> pathSum(TreeNode root, int sum) {
  4. List<Integer> curres = new ArrayList<Integer>();
  5. help(root,0,sum,curres);
  6. return res;
  7. }
  8. private void help(TreeNode root,int sum ,int target,List<Integer> curres){
  9. if(root == null) return ;
  10. curres.add(root.val);
  11. if(root.left==null && root.right==null && sum+root.val==target)
  12. res.add(new ArrayList(curres));
  13. help(root.left,sum+root.val,target,curres);
  14. help(root.right,sum+root.val,target,curres);
  15. curres.remove(curres.size()-1);
  16. }
  17. }

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. Bootstrap打印问题

    删除bootstrap的样式引用,就可以正常打印预览了. bootstrap 设置了@media print相关属性导致 @media print { * { color: #000 !importa ...

  2. js json 单双引号互换

    var _adrobj = JSON.stringify(address).replace(/\"/g,"'"); var _nstr = _adrdata.replac ...

  3. linux机器之间配置ssh无密访问

    首先确认已安装了ssh服务,没装的自行百度一下. A机器:192.168.1.1 B机器:192.168.1.2 使A无密访问B,步骤如下[root@localhost ~]# cd .ssh 如果没 ...

  4. 【BZOJ1816】[Cqoi2010]扑克牌 二分

    [BZOJ1816][Cqoi2010]扑克牌 Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张j ...

  5. H5页面在微信中禁止下拉露出网页

    H5页面在微信中禁止默认事件的执行,js添加代码 $(function () { /************微信h5页面禁止下拉露出网页来**************/ $('body').on('t ...

  6. bootstrap 媒体查询

    //各类设备的分辨率 /*超小设备(手机,小于768px)*/ /* Bootstrap 中默认情况下没有媒体查询 */ /*超小型设备(小于768px)*/ @media (min-width:@s ...

  7. 动态代理:JDK原生动态代理(Java Proxy)和CGLIB动态代理原理+附静态态代理

    本文只是对原文的梳理总结,以及自行理解.自己总结的比较简单,而且不深入,不如直接看原文.不过自己梳理一遍更有助于理解. 详细可参考原文:http://www.cnblogs.com/Carpenter ...

  8. Pycharm如何取消自动换行

    1.只对当前文件有效的操作是: 菜单栏->View -> Active Editor -> Use Soft Wraps (不选中) 2.要是想对所有文件都起到效果,就要在setti ...

  9. python之MySQL学习——输出指定条件的结果集

    # 引入pymysql模块 import pymysql as pm # 数据库连接 db = pm.connect(host=",database='task', charset='utf ...

  10. 【转】Startssl SSL 证书申请图解

    一.什么是 SSL 证书,什么是 HTTPS 网站? SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secu ...