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 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]
]
SOLUTION 1:
使用递归解决,先把下一个可能要加的节点加入到path中,再使用递归依次计算即可。
/**
* Definition for binary tree
* 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>> ret = new ArrayList<List<Integer>>(); List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return ret;
} path.add(root.val);
sum -= root.val; dfs(root, sum, path, ret); return ret;
} public void dfs(TreeNode root, int sum, List<Integer> path, List<List<Integer>> ret) {
if (root == null) {
return;
} if (sum == 0 && root.left == null && root.right == null) {
ret.add(new ArrayList<Integer>(path));
return;
} if (root.left != null) {
path.add(root.left.val);
dfs(root.left, sum - root.left.val, path, ret);
path.remove(path.size() - 1);
} if (root.right != null) {
path.add(root.right.val);
dfs(root.right, sum - root.right.val, path, ret);
path.remove(path.size() - 1);
}
}
}
SOLUTION 2:
使用递归解决,如果只考虑加入当前节点,会更加简单易理解。递归的base case就是:
1. 当null的时候返回。
2. 当前节点是叶子 并且sum与root的值相同,则增加一个可能的解。
3. 如果没有解,将sum 减掉当前root的值,并且向左树,右树递归即可。
// SOLUTION 2
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ret = new ArrayList<List<Integer>>(); List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return ret;
} dfs2(root, sum, path, ret); return ret;
} public void dfs2(TreeNode root, int sum, List<Integer> path, List<List<Integer>> ret) {
if (root == null) {
return;
} path.add(root.val);
sum -= root.val;
if (sum == 0 && root.left == null && root.right == null) {
ret.add(new ArrayList<Integer>(path));
} else {
dfs2(root.left, sum, path, ret);
dfs2(root.right, sum, path, ret);
} path.remove(path.size() - 1);
}
LeetCode: Path Sum II 解题报告的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [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】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】666. Path Sum IV 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [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] 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】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
随机推荐
- 如何将jar包加入到Maven本地仓库
原则上Maven的设计是不需要这么做的,因为pom.xml中依赖的jar包会自动实现从中央仓库下载到本地仓库.但是公司设计了一个setting,如果本地仓库没有,就去setting指定的url中下载j ...
- testng.xml顺序执行多个case配置
testng.xml顺序执行多个case配置 项目结构如图:
- python 处理中文文件时的编码问题,尤其是utf-8和gbk
python代码文件的编码 py文件默认是ASCII编码,中文在显示时会做一个ASCII到系统默认编码的转换,这时就会出错:SyntaxError: Non-ASCII character.需要在代码 ...
- Swift闭包概念与常见使用场景总结
·Swift 闭包 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks)以及其他一些 ...
- t-sql或mssql怎么用命令行导入数据脚本
osql简单用法:用来将本地脚本执行,适合sql脚本比较大点的情况,执行起来比较方便 osql -S serverIP -U sa -P 123 -i C:\script.sql serverIP数据 ...
- 03-Vue入门系列之Vue列表渲染及条件渲染实战
3.1. 条件渲染 有时候我们要根据数据的情况,决定标签是否进行显示或者有其他动作.最常见的就是,表格渲染的时候,如果表格没有数据,就显示无数据.如果有数据就显示表格数据. Vue帮我们提供了一个v- ...
- [ACM_图论] Highways (变形说法的最小生成树)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28972#problem/C 题目给出T种情况,每种情况有n个城镇,接下来每一行是第i个城 ...
- [BTS] RFC IDOC_INBOUND_ASYNCHRONOUS
Error Message: Log Name: ApplicationSource: BizTalk ServerDate: 9/10/2013 3:56: ...
- 使用socket.io开发简单群聊功能
1.新建package.json文件: { "name": "socket-chat-example", "version": " ...
- Android 学习之--android多线程断点下载
我们平时都用"迅雷"下载软件,当下载到一半的时候突然断网,下次开启的时候能够从上次下载的地方继续下载,而且下载速度很快,那么这是怎么做到的呢! 其实它的“快”其实就是多线程的下载实 ...