【Leetcode】113Path Sum II
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的更多相关文章
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【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 ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【leetcode】Combination Sum II (middle) ☆
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【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 ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
随机推荐
- MFC 程序退出方法
基於對話框的: 1.PostQuitMessage(0);2.PostMessage(WM_QUIT,0,0);3.ExitProcess(0);注意使用时先释放分配的内存,以免造成内存泄露4.exi ...
- 20155212Arrays和String测试_MySort
Arrays和String单元测试 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arrays类 s ...
- 20155230 2016-2017-2《Java程序设计》第二周学习总结
20155230 2016-2017-2 <Java程序设计>第er周学习总结 教材学习内容总结 JAVA编程风格 1.命名变量时不可以使用数字及特殊字符作为开头. 2.变量名称不可以与J ...
- C语言复习20170716
C语言复习20170716 C数据类型 图片来自:C语言基本数据类型简介 C语言程序处理的数据有常量和变量两种形式. 常量是在程序中不能改变其值的量.例如:整型常量.实型常量.字符常量.字符串常量和枚 ...
- 20155325 加分作业 实现pwd
要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 准备 思路 问题 1.如何获取当前目录的节点号 Linux ...
- 20145209 实验三 《敏捷开发与XP实践》 实验报告
20145209 实验三 <敏捷开发与XP实践> 实验报告 实验内容 XP基础. XP核心实践. 相关工具. 实验步骤 敏捷开发与XP 1.敏捷开发 敏捷开发(Agile Developm ...
- 树链剖分学习&BZOJ1036
题目传送门 树链剖分,计算机术语,指一种对树进行划分的算法,它先通过轻重边剖分将树分为多条链,保证每个点属于且只属于一条链,然后再通过数据结构(树状数组.SBT.SPLAY.线段树等)来维护每一条链. ...
- GridSQL--Stado 学习初步
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页 作者 高健@博客园 luckyjackgao ...
- Jumpserver跳板机入门
1.jumpserver安装 1.1.环境介绍 系统: CentOS 7.4.1708IP: 192.168.56.110 [root@linux-node1 ~]# uname -r -.el7.x ...
- Angular ng-include 学习实例
ng-include 可以引入外部的文件到当前视图中.这样可以增强复用性. 最简单的用法 <div ng-include src="'/public/template/tpl.htm ...