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 日期 题目地 ...
随机推荐
- my sql
如果改了上面的4个配置文件,要让其立即生效,可以使用如下方法 source .bash_profile . .bash_profile 基于Apache+php+mysql的许愿墙网站的搭建 方案一: ...
- Android--UI
1.layout_width 属性和 layout_height 属性:Android中所有的控件都包含这两个属性,有三种可选值 match_parent, fill_parent, wrap_con ...
- 使用VS2013在WIN8.1上运行gaclib的hello world
首先:gaclib的官网是http://www.gaclib.net/ 需要了解更多信息的请自己去官网,我也是刚刚研究 第一步 下载gaclib的源码 这些文件是运行程序所必须的 第二步 ...
- iOS——Command-Line 查看当前SDK版本并修改默认SDK版本
在工作中可能会碰到用命令行编译.打包iOS应用程序的情况(xcodebuild相关命令). 但是由于SDK版本问题,会报错,说某SDK版本不对,可能是因为升级Xcode导致的SDK版本升级,为了避免高 ...
- 基于D3JS绘制中国地图
仿照D3JS官网上的美国地图制作了一个中国版的地图. D3JS官网上的版本: http://bl.ocks.org/NPashaP/a74faf20b492ad377312 中国版的地图效果: 如要制 ...
- 通过JavaScript脚本实现验证码自动输入
很多网站在用户进行某次点击,比如在线购物确认购买时,会要求用户输入验证码,这在一般情况下也没啥问题,但在用户需要频繁购买或是抢购时就很讨厌了.其实网站的验证码一般是由JS脚本生成的,因此也可以通过编写 ...
- 【原】对频率论(Frequentist)方法和贝叶斯方法(Bayesian Methods)的一个总结
注: 本文是对<IPython Interactive Computing and Visualization Cookbook>一书中第七章[Introduction to statis ...
- MySQL group_concat 1024 大小
1. GROUP_CONCAT有个最大长度的限制,超过最大长度就会被截断掉,你可以通过下面的语句获得: SELECT @@global.group_concat_max_len; show varia ...
- Linux下的NFS配置(转)
http://rubyer.me/blog/1682/ 遇到的问题: 1.reason given by server: Permission denied 在服务器的/etc/export配置文件中 ...
- verify.js使用验证插件使用
github:https://github.com/52fhy/verify.js 首先引入js,最好拷贝verify整个目录,因为里面有图标. <script src="verify ...