[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 ...
随机推荐
- SqlParameter 多个参数动态拼接解决参数化问题
多个参数化是固定比较easy,多个动态的就有点...工作中遇到的问题整理下来分享 ,上代码 SqlParameter[] param = new SqlParameter[] { }; List< ...
- NPOI导入导出EXCEL通用类,可直接使用在WinForm项目中
由于XSSFWorkbook类型的Write方法限制,Write完成后就自动关闭流数据,所以无法很好的支持的Web模式,网上目前也未找到好的解决方案. 注意:若直接使用在WinForm项目中,必需先下 ...
- 免费api
聚合数据提供30大类,100种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免费的各类API接口. 聚合的免费API接口数据: 手机号码归属地API接口:https://www. ...
- 【Python】【有趣的模块】tqdm | inspect
tqdm """ [tqdm] 显示循环的进度条,再也不用担心程序跑到哪里还要跑多久了 tqdm 可以直接包裹iterable对象 from tqdm import tq ...
- Eclispe中编辑xml配置文件时不会提示也不能自动调整格式
创建了一个xml文件后,发现编辑起来和原来的那些有所不同,不会提示补全.也不能自动调整格式???woc? 哈哈哈哈哈,“我最恨你像个石头一样” 后来发现是编辑器被改了!!! 右键xml文件然后open ...
- 学习笔记49—matlab FDR校正
matlab自带函数mafdr,当ttest数较多时,可直接用[FDR, Q]=mafdr(P):但是Storey procedure在p值少于1000个时会崩溃,此时应改用BH FDR方法:mafd ...
- angular7 + d3 显示svg
汇总一些之前没有注意到的问题 总体思路: app只是显示svg为主,接收后端推送的数据改变,显示变化后的svg. 因此,只用d3的数据绑定更新组件里<svg></svg>节点. ...
- Qt5数据库
对于习惯使用SQL语法的用户,QSqlQuery类提供了直接执行SQL语句并处理返回结果的方法.对于习惯使用较高层数据库接口避免使用SQL语句的用户,QSqlTableModel类和QSqlRela ...
- VMware vSphere client 中英文语言界面设置
安装的时候可以选择简体中文,然后安装后,对vsphere client的执行程序制作快捷方式 以win7 x64环境默认路径为例 英文启动如下: "C:Program Files (x86) ...
- mq/mysql/redis/nginx常见服务&工具安装
单机版 3.1安装工具 3.1.1 安装Maven工具 3.1.1上传安装包 1)root用户创建安装目录如/usr/local /maven: mkdir -p /usr/local/maven ...