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. dedecms安全篇:织梦文件夹目录权限设置

    织梦各个目录安全详解   做织梦(dedecms)网站安全必看1.a  因为是静态目录,并且在要生成HTML的,所以拒绝脚本执行  允许写入2.data   因为是缓存等,所以充许写入,但是因为这里面 ...

  2. 虚拟机(VMware Workstation)安装Ubuntu简易安装

    1.安装虚拟机 这里我安装的是:VMware Workstation v12.1.0 官方简体中文版 地址:http://www.3987.com/xiazai/1/12/37116.html#dow ...

  3. (转)reactor模式

    转自: http://www.blogjava.net/DLevin/archive/2015/09/02/427045.html Reactor模式详解 前记 第一次听到Reactor模式是三年前的 ...

  4. Android基础新手教程——3.1 基于监听的事件处理机制

    Android基础新手教程--3.1.1 基于监听的事件处理机制 标签(空格分隔): Android基础新手教程 本节引言: 第二章我们学习的是Android的UI控件,我们能够利用这些控件构成一个精 ...

  5. EntityFramework增删改查

    http://www.cnblogs.com/libingql/archive/2013/01/29/2881988.html

  6. 机器学习框架MXnet安装步骤

    安装环境:redhat7.1+vmw 安装步骤: # Install git if not already installed. sudo yum -y install git-all# Clone ...

  7. 52、图片缩放库 PhotoView

    PhotoView的简介: 这是一个图片查看库,实现图片浏览功能,支持pinch(捏合)手势或者点击放大缩小.支持在ViewPager中翻页浏览图片. PhotoView 是一款扩展自Android ...

  8. 【BZOJ1922】[Sdoi2010]大陆争霸 Dijkstra

    Description 具体地说,杰森国有 N 个城市,由 M条单向道 路连接.神谕镇是城市 1而杰森国的首都是城市 N.你只需摧毁位于杰森国首都 的曾·布拉泽大神殿,杰森国的信仰,军队还有一切就都会 ...

  9. quartz定时任务配置

    参考:http://www.cnblogs.com/kay/archive/2007/11/02/947372.html Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quar ...

  10. 拨打电话<a href="tel:">跳转到邮件<a href="mailto:">

    拨打电话 <a href="tel:0571866000">0571-866000</a> 跳转到邮件 <a href="mailto:jo ...