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 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]
]
题解:
与上一题不同的是,要存储所有的root - leaf路径,所以在DFS的基础上我们还要增加backtracking。回溯的点有两个,一个是当前root满足条件,返回时,此时回溯保证当前root的sibling结果正确; 另外一个是遍历完当前root的左子树和右子树时, 这时回溯也是保证sibling结果正确。比如 root = [0, 1, 1], sum = 1,则计算完左边的1时可以正确计算到右边的1。或者 root = [0, 1, 1, 0, 0], sum = 1, 遍历完左边的两个0后可以正确计算到右边1的结果。 (有空要组织一下语言)
Time Complexity - O(n), Space Complexity - O(n)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, root, sum);
return res;
} private void dfs(List<List<Integer>> res, ArrayList<Integer> list, TreeNode root, int sum) {
if(root == null)
return;
if(root.left == null && root.right == null && root.val == sum) {
list.add(root.val);
res.add(new ArrayList<Integer>(list));
list.remove(list.size() - 1);
return;
} list.add(root.val);
sum -= root.val;
dfs(res, list, root.left, sum);
dfs(res, list, root.right, sum);
list.remove(list.size() - 1);
}
}
二刷:
注意回溯的点。
Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
pathSum(res, list, root, sum);
return res;
} private void pathSum(List<List<Integer>> res, List<Integer> list, TreeNode root, int sum) {
if (root == null) return;
sum -= root.val;
list.add(root.val);
if (root.left == null && root.right == null && sum == 0) {
res.add(new ArrayList<>(list));
list.remove(list.size() - 1);
return;
}
pathSum(res, list, root.left, sum);
pathSum(res, list, root.right, sum);
list.remove(list.size() - 1);
}
}
测试:
113. Path Sum II的更多相关文章
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 【LeetCode】113. 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] 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 113. Path Sum II (Tree; DFS)
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 (路径和) 解题思路和方法
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++ ...
随机推荐
- CSDN 自动评论
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/csdn-auto-reviews/ 说明 当打开http://download.csdn.net/my/do ...
- js文件内部导入引用js文件方法
function include(path){ var a=document.createElement("script"); a.type = "te ...
- Ajax 之【文件上传】
// 前台 var formData = new FormData(); var file = document.getElementById('myFile').files[0]; formData ...
- Python print语句
1. 输出字符串 >>> strHello = 'Hello World' >>> print (strHello) Hello World 2. 格式化输出整数 ...
- HDU 4548 美素数
Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“ ...
- android service 整理
项目经常要跟别的项目进行交互,比如说蓝牙打印机等,或者处理一些网络状态,或者调用baidu.高德等地图的时候就会用到, 或打开了音乐播放之后,便想去看看图片,或者下载文件的时候,我们看看博客. Ser ...
- EXTJS 4.2 资料 控件之Grid Columns 列renderer 绑定事件
columns: [ { header: '序号', xtype: 'rownumberer', align: 'center', width: 100 }, { header: 'CompanyId ...
- Hibernate从入门到精通(九)一对多双向关联映射
上次的博文Hibernate从入门到精通(八)一对多单向关联映射中,我们讲解了一下一对多单向映射的相关内容,这次我们讲解一下一对多双向映射的相关内容. 一对多双向关联映射 一对多双向关联映射,即在一的 ...
- 【BZOJ 1084】[SCOI2005]最大子矩阵
Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤2 ...
- 读书笔记汇总 --- 用Python写网络爬虫
本系列记录并分享:学习利用Python写网络爬虫的过程. 书目信息 Link 书名: 用Python写网络爬虫 作者: [澳]理查德 劳森(Richard Lawson) 原版名称: web scra ...