[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 ...
随机推荐
- 用Let's Encrypt实现Https(Windows环境+Tomcat+Java)
补充1: 已解决20的部分问题,移步这里 单域名下多子域名同时认证HTTPS 补充2: 之前忘了说了,我这个方法只对Tomcat7.0以上有用(要不然就是8.0...) 我自己用的是9.0 原因好像是 ...
- Linux let 命令
命令:let let 命令是 BASH 中用于计算的工具,用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量.如果表达式中包含了空格或其他特殊字符,则必须引起来. 语法格式 let arg ...
- React Native组件之BackAndroid !安卓手机的物理返回键的使用
ok!在安卓手机上,当我们用物理返回键的时候,会以一次性的将程序退出来,这样是很不好的体验,所以就需要使用RN的物理返回键组件:BackAndroid,其原理也就是 分析路由,然后pop()这样! o ...
- TypeError: add() argument after * must be an iterable, not Settings的错误原因
在抄代码的时候发现有个错误: TypeError: add() argument after * must be an iterable, not Settings 看不懂,百度才知道原因,原来是第2 ...
- 如何看待Arcsoft虹软,推出的人脸认知引擎免费SDK?
虹软公司是一家具有硅谷背景的图像处理公司,除了人脸技术以外,还有多项图像及视频处理技术.他们的双摄像头处理算法和人脸美化算法囊括了包括OPPO VIVO,SUMAMNG一系列手机厂商. 整个人脸识别运 ...
- kbengine学习2 创建项目
官方文档https://www.comblockengine.com/docs/1.0/get-started/createproject/ 1.kbe服务器端 1.1 复制出一个assets文件夹, ...
- CC2 条理分明-----独立思考
独立思考 前几天啊,在吃饭的时候,听到同事们在讨论某幼儿园事件,因为没有人愿意出来作证,所以很可能是造谣.前几天他们还咬牙切齿的指责这个幼儿园,现在怎么就变了.我发现人们的思维变化的太快,我自己也是的 ...
- spring cloud: 关闭ribbon负载均衡
spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...
- clustering
搞了将近一年的单细胞,聚类也是自认为得心应手了,自信满满. 但是多半是跑软件,对聚类的深层次的思想不甚了了. Google了一下clustering,看了一篇文章,突然了解到了clustering的算 ...
- Hibernate二级缓存 --Hibernate框架
Hibernate本身只提供了二级缓存的规范,但并未实现,所以需要第三方缓存插件的支持.常用的二级缓存第三方插件有:EHCache.Memcached.OSCache.SwarmCache.JBoss ...