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. Linux系统下连接校园网Drcom客户端教程(广东工业大学)

    这篇教程写给想要学习Linux系统或者在Linux系统下有需要使用Drcom上网的同学,在我疯狂踩坑,经过n多次的刷机装机实验,体验不同发行版本的linux系统后,终于懂得怎么连接上drcom,想想连 ...

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

    20155307 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 所谓继承,讲的就是出现很多很多相同的部分的话,就把这个部分变成"父类", ...

  3. spring boot启动报内存溢出的问题

    问题: springBoot项目,已经两次了,启动报内存溢出,内存泄露 分析: 内存泄露是因为垃圾回收器想要回收程序不用的对象,但是该对象还有引用存在 解决: 1.第一次是mybatis文件和Java ...

  4. struts2框架实例

    一,Struts2框架介绍 它是一个View框架,对Servle进行了封装,使用核心过滤器对servlet进行了解耦,可以自动封装数据 核心是结果视图导航 二,程序实例 1.导入框架依赖包 2.注册框 ...

  5. 【转载】值得推荐的C/C++框架和库

    原文:值得推荐的C/C++框架和库 值得学习的C语言开源项目 Libevent libev是一个开源的事件驱动库,基于epoll,kqueue等OS提供的基础设施.其以高效出名,它可以将IO事件,定时 ...

  6. ELKStack入门篇(一)之ELK部署和使用

    一.ELKStack简介 1.ELK介绍 中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details ELK Stack包 ...

  7. 原生与JS交互 iOS

      前言 Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体验的优势”和“Web App跨平台开发的优势” ...

  8. protobuf工程的编译以及使用

    一. 获取Protocol Buffer 1.1 获得源码 Github:ProtocolBuffer源码 Git clone之:git clone https://github.com/google ...

  9. 我们一起学习WCF 第七篇会话模式

    会话:就是客户端和服务端之间的谈话.比喻A和B去登陆网站,那么A用户登陆进去肯定显示A的用户详情,那么这就是A和服务器之间的交流.同样B用户登陆之后显示B的详情,这就表示这是B和服务器之间的交流. 如 ...

  10. linux安装PHP-memcache-redis扩展

    1.php memcache 扩展 http://pecl.php.net/package/memcache/3.0.8 下载文件源码 #tar zxvf memcache-3.0.8.tar#/us ...