[LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结
描述
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]
]
解析
除了要判断是否有这样的一个path sum,还需要把所有的都可能性结果都返回,所以就用传统的DFS递归解决子问题。
将当前节点root的值放入list中更新sum值,判断当前节点是否满足递归条件root.left == null && root.right == null&&sum == 0;
若满足,则将存有当前路径的list值存入最后的大list中
然后依次递归左子树和右子树
从存有当前路径的list中去除最后一个节点,这样便可以返回到了当前叶子节点的父节点
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<List<Integer>> listAll = new ArrayList<>();
List<Integer> list = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if(root == null)
return listAll;
list.add(root.val);
sum -= root.val;
if(root.left == null && root.right == null && sum == 0)
listAll.add(new ArrayList<Integer>(list));
pathSum(root.left, sum);
pathSum(root.right, sum);
list.remove(list.size() - 1);
return listAll;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void pathSumHelper(TreeNode root, int sum, List<Integer> sumlist, List<List<Integer>> pathlist) {
if (root == null)
return;
sumlist.add(root.val);
sum = sum - root.val;
if (root.left == null && root.right == null) {
if (sum == 0) {
pathlist.add(new ArrayList<Integer>(sumlist));
}
} else {
if (root.left != null)
pathSumHelper(root.left, sum, sumlist, pathlist);
if (root.right != null)
pathSumHelper(root.right, sum, sumlist, pathlist);
}
sumlist.remove(sumlist.size() - 1);
} public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> pathlist = new ArrayList<List<Integer>>();
List<Integer> sumlist = new ArrayList<Integer>();
pathSumHelper(root, sum, sumlist, pathlist);
return pathlist;
}
}
[LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)的更多相关文章
- [LeetCode] 113. 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] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode 113. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- leetcode 113. 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] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- [leetcode]113. 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 113 path Sum II 路径和
递归先序遍历+vector<int>容器记录路径 /** * Definition for a binary tree node. * struct TreeNode { * int va ...
- Leetcode 113. 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 ...
随机推荐
- CSS sprites
CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式. 优点: 它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一 ...
- hdu 3832 Earth Hour bfs
Earth Hour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Prob ...
- 浅谈Linux文件系统
Linux 与其他类 UNIX 系统一样并不区分文件与目录:目录是记录了其他文件名的文件.使用命令 mkdir 创建目录时,若期望创建的目录的名称与现有的文件名(或目录名)重复,则会创建失败. Lin ...
- 如何连接oracle 12c可插拔数据库
启动根容器:[oracle@eric ~]$ export ORACLE_SID=cup[oracle@eric ~]$ sqlplus / as sysdbaSQL*Plus: Release 12 ...
- Java——String,StringBuffer,StringBuilder
String 一经创建,不可更改,每次更改都是创建新对象,销毁旧对象 StringBuilder 创建后可修改,多线程不安全 StringBuffer 创建后可修改,多线程安全 StringBuffe ...
- Utunbu常见问题
关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案 https://blog.csdn.net/u011596455/article/details/ ...
- python 修改excel
操作描述:需要实现数据不断写入的功能,首先,在固定位置建立一个空白的xls文件:其次,每次产生的数据先判断该xls已有几列数据,后缀上去. 具体过程: 要保证具有三个包,是xlrd,xlwt,xlut ...
- stop 用法
1. stop 文档 $(selector).stop(stopAll,goToEnd) stopAll 可选.规定是否停止被选元素的所有加入队列的动画.goToEnd 可选.规定是否允许完成当前的动 ...
- 牛客网NOIP赛前集训营-提高组(第一场)A 中位数
中位数 思路: 二分答案 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include< ...
- [Spring] ClassPathXmlApplicationContext类
1. 该类在package org.springframework.context.support包下. 该包在4.0.1中封装在spring-context-***.jar中. 其无参构造函数的文档 ...