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 解题报告的更多相关文章

  1. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  2. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  3. [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 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】666. Path Sum IV 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  6. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  7. [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 ...

  8. [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 ...

  9. 【LeetCode】437. Path Sum III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...

随机推荐

  1. day4----生成器,迭代器

    迭代器,生成器,装饰器 1.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要 ...

  2. Android--入门

    之前自己在学校写过一些安卓应用,那时候没有系统地学过安卓,用到什么就网上找博客.找Demo,然后自己跟着敲一遍,有些东西也不太理解,现在打算做android开发这一块了,趁毕业之前赶紧多学些技术.先是 ...

  3. C3P0连接池异常

    解决方案: 将c3p0.jar包换成c3p0-0.9.0.2.jar,c3p0这个包应该有bug 引用如下: com.mchange.v2.log.MLog Determines which libr ...

  4. 使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)

    使用java 程序创建格式为utf-8文件的方法:  try{            File file=new   File("C:/11.jsp");              ...

  5. SQL Server优化器特性-隐式谓词

    我们都知道,一条SQL语句提交给优化器会产生相应的执行计划然后执行输出结果,但他的执行计划是如何产生的呢?这可能是关系型数据库最复杂的部分了.这里我为大家介绍一个有关SQL Server优化器的特性- ...

  6. PyCharm 4.5.4 环境配置

    1.去掉“自动保存功能” pycharm默认是自动保存的,习惯自己按 ctrl + s 的可以进行如下设置: 菜单File -> Settings... -> Appearance &am ...

  7. [ACM_水题] ZOJ 3712 [Hard to Play 300 100 50 最大最小]

    MightyHorse is playing a music game called osu!. After playing for several months, MightyHorse disco ...

  8. [BTS] The external credentials in the SSO database are more recent.

    For test some interface. We change BizTalk Server time to a feature date. For example, change to nex ...

  9. java 32位MD5加密的大写字符串

    package com.aok.test; import java.security.MessageDigest; public class MD5Test { public static void ...

  10. Hibernate关联映射(转载)

    原文:http://www.cnblogs.com/huxi/archive/2009/12/15/1624988.html 以简单的两个类为例: User(int id, String name)  ...