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. 4.20-4.24程序设计基础结束,UID课程

    通过其他方式实现string函数的效果,效果有比较数组字符.显示字符串长度.复制字符串等.在比较字符串的时候,首先是比较字符串的长度,当长度一样的时候进行不同位置上一一对应的字符比较大小.关于字符长度 ...

  2. delphi 获取两个颜色差值

    前面说了已经获取到颜色值了,现在需要比较两个颜色的差值. 两个颜色的根据RGB的差来取,有两种情况: 1.(R的平方+G的平方+B的平方)开根号,再两个颜色值相减获取差值. 2.(((R1-R2)的平 ...

  3. solr与.net系列课程(九)solr5.1的配置

    solr与.net系列课程(九)solr5.1的配置 最近一些园友来咨询solr5.1的配置方式,然后我就去官网下载了个最新版本的solr,发现solr5.0以后solr的下载包里的内容发生的变化,移 ...

  4. Kali Linux系列教程之OpenVas安装

    Kali Linux系列教程之OpenVas安装 文 /玄魂 目录 Kali Linux系列教程之OpenVas安装 前言 1.  服务器层组件 2.客户层组件 安装过程 Initial setup ...

  5. Android布局中涉及的一些属性

    Android:gravity属性 线性布局常见的就是利用LinearLayout进行布局,其中有个比较重要的属性就是android:gravity,在官方文档中是这么描述这个属性的:指定一个元素怎么 ...

  6. Nginx学习笔记(一) Nginx架构

    Nginx架构 Nginx全程是什么? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. ...

  7. [ucgui] 彩色条函数

    /* 颜色条 */ void ShowColorBar(void) { , y0 = , yStep = , i; int NumColors = LCD_GetDevCap(LCD_DEVCAP_N ...

  8. 利用nodejs模块缓存机制创建“全局变量”

    在<深入浅出nodejs>有这样一段(有部分增减): 1.nodejs引入模块分四个步骤 路径分析 文件定位 编译执行 加入内存 2.核心模块部分在node源代码的编译过程中就编译成了二级 ...

  9. paip.最省内存的浏览器评测 cah

    paip.最省内存的浏览器评测 cah 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/at ...

  10. 安装Vmware workstation虚拟机(含软件和注册码)

    1:虚拟机的安装步骤,包含软件包和注册码.本博客所使用的虚拟机版本是vmware-workstation_11.1.0版本,注意不是最新版本, 软件包:http://pan.baidu.com/s/1 ...