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. Python_ip代理

    #encoding=utf8import urllibimport urllib2import sys sys.path.append('D:/python/beautifulsoup')sys.pa ...

  2. 下列哪个为JSP的隐含对象。

    下列哪个为JSP的隐含对象. A.env B.page C.jspinfo D.context 解答:B JSP有九个隐士对象 request对象:保存了很多客户端请求的信息. response对象: ...

  3. Unity3D GUI图形用户界面系统

    1.skin变量 using UnityEngine; using System.Collections; public class Skin : MonoBehaviour { public GUI ...

  4. Linux IO操作——RIO包

    1.linux基本I/O接口介绍 ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, void *buf, siz ...

  5. js for in

    JavaScript中for..in循环陷阱 大家都知道在JavaScript中提供了两种方式迭代对象:   (1)for 循环:   (2)for..in循环: 使用for循环进行迭代数组对象,想必 ...

  6. ios 页面过长卡顿的情况

    解决方案如下: -webkit-overflow-scrolling: touch;

  7. TF-IDF(词频-逆向文件频率)用于文字分类

    SVM分类器:支持向量机Support Vector Machine. 一个普通的SVM就是一条直线,用来完美划分linearly separable的两类.解决线性 要解决非线性需要到高维处理: 核 ...

  8. React资料

    基于ReactNative开发的APPhttp://reactnative.cn/cases.htmlhttp://www.cnblogs.com/qiangxia/p/5584622.html F8 ...

  9. Node.js 入门 资源

    Node.js 入门 <快速搭建 Node.js 开发环境以及加速 npm> http://fengmk2.com/blog/2014/03/node-env-and-faster-npm ...

  10. jQery 操作CSS

    jQuery操作CSS也是很方便的,咱先看看这几个常用的方法: addClass():向一个元素添加一个或者多个类. removeClass():从一个元素中删除一个类或多个类. toggleClas ...