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. A1038

    用一串数拼接成一个数,输出最小的. 思路:使用string和相关的函数. #include<iostream> #include<cstdio> #include<str ...

  2. Hadoop命令大全

    Hadoop命令大全 分类: 云计算2011-03-01 15:04 6852人阅读 评论(0) 收藏 举报 hadoop作业任务集群class脚本 1.列出所有Hadoop Shell支持的命令   ...

  3. 20155227 《Java程序设计》实验五 Java网络编程及安全实验报告

    20155227 <Java程序设计>实验五 Java网络编程及安全实验报告 实验内容 任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能. 编写MyDC.java实现从上面 ...

  4. WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书

    原文:WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书 最近项目中使用弹出控件Popup,发现弹出框的对齐方式在不同的系统中存在不同(Popup在win10上是 ...

  5. Cache-Aside模式

    Cache-Aside 该模式是从数据仓库中将数据加载到缓存中,从而提高访问速度的一种模式.该模式可以有效的提高性能,同时也能一定程度上保证缓存中的数据和数据仓库中的数据的一致性,和同步数据到数据仓库 ...

  6. Luogu1801_黑匣子_KEY

    题目传送门 借这道题练一下Treap和Splay的板子. code: #include <cstdio> #include <cstdlib> using namespace ...

  7. 【LG4317】花神的数论题

    [LG4317]花神的数论题 题面 洛谷 题解 设\(f_{i,up,tmp,d}\)表示当前在第\(i\)位,是否卡上界,有\(tmp\)个一,目标是几个一的方案数 最后将所有\(d\)固定,套数位 ...

  8. 用php做个简单的日历

    存档: index.php <html> <head> <title>日历</title> <style> table{border:1px ...

  9. 180718-jar包执行传参使用小结

    jar包执行时传参的使用姿势 虽说我们现在大多不太直接使用jar包运行方式,目前比较主流的是将自己的服务丢在某个容器中(如tomcat,jetty等)运行,比如我之前所属的电商公司,就是将项目打包为w ...

  10. charles基本使用文档

    Charles 主要的功能包括: 截取 Http 和 Https 网络封包. 支持重发网络请求,方便后端调试. 支持修改网络请求参数. 支持网络请求的截获并动态修改. 支持模拟慢速网络. Charle ...