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]
]

Tips:在112题的基础上,加深难度,本题要求输出和为sum的树中的所有路径。

本题要注意List<Integer> 是 List<List<Integer>> 的一个元素。要求出所有路径,需要每找到一条List<Integer>类型的路径,就将其添加到 List<List<Integer>>集合中。

package medium;

import java.util.ArrayList;
import java.util.List; public class L113PathSumII {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> path = new ArrayList<>();
List<Integer> oneline = new ArrayList<>();
if (root == null)
return path;
int ans = 0;
hasPathSumCore(root, sum, path, ans,oneline);
return path;
} private List<List<Integer>> hasPathSumCore(TreeNode root, int sum, List<List<Integer>> path, int ans,List<Integer> oneline) {
ans += root.val;
oneline.add(root.val);
if (ans == sum && root.left == null && root.right == null) {
path.add(new ArrayList<Integer>(oneline));
}
if (root.left != null) {
hasPathSumCore(root.left, sum,path, ans,oneline);
}
if (root.right != null) {
hasPathSumCore(root.right, sum,path, ans,oneline);
}
ans -= root.val;
oneline.remove(oneline.size()-1);
return path; } public static void main(String[] args) {
TreeNode root = new TreeNode(5);
TreeNode node1 = new TreeNode(4);
TreeNode node2 = new TreeNode(8);
TreeNode node3 = new TreeNode(11);
TreeNode node4 = new TreeNode(13);
TreeNode node5 = new TreeNode(4);
TreeNode node6 = new TreeNode(7);
TreeNode node7 = new TreeNode(2);
TreeNode node8 = new TreeNode(1);
root.left = node1;
root.right = node2;
node1.left = node3;
node2.left = node4;
node2.right = node5;
node3.left = node6;
node3.right = node7;
node5.right = node8; node1.right = null;
node6.left = null;
node6.right = null;
node7.left = null;
node7.right = null;
node4.left = null;
node4.right = null;
node5.left = null;
node8.left = null;
node8.right = null;
int sum = 22; L113PathSumII l113 = new L113PathSumII();
List<List<Integer>> ans =new ArrayList();
ans=l113.pathSum(root, sum);
for(int i=0;i<ans.size();i++){
List<Integer> iter=ans.get(i);
for(int j=0;j<iter.size();j++){
System.out.println(iter.get(j));
} }
System.out.println("HHHHHHHHHHHHH");
TreeNode root1 = new TreeNode(-2);
TreeNode root2 = new TreeNode(-3);
root1.left = null;
root1.right = root2;
root2.left = null;
root2.right = null;
ans=l113.pathSum(root1, -5);
for(int i=0;i<ans.size();i++){
List<Integer> iter=ans.get(i);
for(int j=0;j<iter.size();j++){
System.out.println(iter.get(j));
} }
}
}

【Leetcode】113Path Sum II的更多相关文章

  1. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  2. 【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 ...

  3. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  4. 【leetcode】Combination Sum II (middle) ☆

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode】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】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  7. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  8. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  9. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

随机推荐

  1. ubuntu 和windows 分别在anaconda上安装tensorflow

    windows下 的anaconda安装tensorflow: 在Anaconda Prompt中:conda install tensorflow python=3.5一直下载失败.总结一下原因可能 ...

  2. Asp.Net实现在线人数统计 (转)

    原文件:http://blog.csdn.net/wxd_860825/article/details/4589292 利用Application对象和Session对象可以统计当前在线用户数量. 注 ...

  3. 一个servlet如何处理多个请求

    页面1:表单的action=login?method=login 页面2:表单的action=login?method=insert ..... 然后通过method的值采用不同方法进行处理. 如下 ...

  4. 20155216 2016-2017-2《Java程序设计》课程总结

    20155216 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:简要内容:我对师生关系的见解 预备作业2:简要内容:有关C语言学习调查以及学习 ...

  5. 20155338 2016-2017-2 《Java程序设计》第4周学习总结

    20155338 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 内容有很多,这里这是选取几个比较重要的一部分来展示. 1.继承 •定义:继承基本上就是避免多 ...

  6. [SDOI2010]地精部落 DP

    LG传送门 DP好题 题意很简单,就是求1-n的排列,满足一个数两边的数要么都比它大要么都比它小,求这样的排列个数对\(p\)取膜的值(为了表述简单,我们称这样的排列为波动序列). 这个题我第一眼看到 ...

  7. L016-linux系统文件权限体系实战深入讲解小节

    L016-linux系统文件权限体系实战深入讲解小节 不知道今天能不能写完哈,能写完发出来就是这周发两次小结了,有进步哦,不过L015和L016两节课内容也确实不多,进入正题 上一课学到了chmod. ...

  8. 搜索引擎ElasticSearch系列(一): ElasticSearch2.4.4环境搭建

    一:ElasticSearch简介 Elasticsearch is a distributed, RESTful search and analytics engine capable of sol ...

  9. web存储机制(localStorage和sessionStorage)

    web存储包括两种:sessionStorage 和 localStorage(都是限定在文档源级别,非同源文档间无法共享) 1.sessionStorage 数据放在服务器上(IE不支持) 严格用于 ...

  10. 常用常忘的delegate,记一下。

    多线程: 1 new Thread(new ThreadStart(Method1))).Start(); 1 new Thread(new ParameterizedThreadStart(Meth ...