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. Java HashMap 源代码分析

    Java HashMap jdk 1.8 Java8相对于java7来说HashMap变化比较大,在hash冲突严重的时候java7会退化为链表,Java8会退化为TreeMap 我们先来看一下类图: ...

  2. PTA基础编程题目集6-4求自定类型元素的平均 (函数题)

    6-4 求自定类型元素的平均 (10 分)  本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType. 函数接口定义: ElementType Aver ...

  3. 使用HtmlAgilityPack将HtmlTable填入DataTable

    HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = hw.Lo ...

  4. 20155331 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    20155331 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验内容及步骤 使用JDK编译.运行简单的java程序 实验目的与要求: 使用JDK和IDE编译.运行简 ...

  5. 银行业务-Excel文件的拆分逻辑

    一.问题: 随着银行业务数据量的急剧增加,原始的人力统计数据已经不能满足要求, 需要开发一款可以实现自动化数据统计的系统平台,进行数据的采集.加工.过滤.统计.预测 其中数据采集方式又以[Excel] ...

  6. 回顾RAC安装过程中对ASM的处理

    1 首先建立好节点间共享的磁盘,要注意从各节点看到的磁盘的序号.名称一致. 2 通过某一个节点,对共享磁盘进行格式化. 3 在Grid Infrastructure 中, 有一个为OCR选择存储介质的 ...

  7. BZOJ4049][CERC2014]Mountainous landscape-[线段树+凸包+二分]

    Description 现在在平面上给你一条折线P1P2P3...Pn. x坐标是严格单调递增的.对于每一段折线PiPi+1,请你找一个最小的j,使得j>i且走在PiPi+1的人能看到折线PjP ...

  8. [hdu6051]If the starlight never fade-[欧拉函数+原根]

    Description 传送门 Solution orz大佬yxq..本题神仙 设g为P的原根. 设$x=g^{a}$,$y=g^{b}$. 由于$(g^{a}+g^{b})^{i}\equiv (g ...

  9. Yii 2.0 中事件的使用

    关于PHP的事件处理,参照 http://www.cnblogs.com/mafeifan/p/4322238.html http://www.cnblogs.com/mafeifan/p/43222 ...

  10. .net core 部署 Docker 所遇到的几个问题

    1.Connection reset by peer 造成这个问题的主要原因是在program.cs 文件中,未加入端口: public static IWebHostBuilder CreateWe ...