题目:

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]
]

链接: http://leetcode.com/problems/path-sum-ii/

题解:

与上一题不同的是,要存储所有的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的更多相关文章

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

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

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

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

  4. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  5. 【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 ...

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

  7. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  10. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

随机推荐

  1. IPoint从自定义的投影坐标系转换到自定义的地理坐标系

    IPoint pointStart = new PointClass(); pointStart = xyPolyline.FromPoint; ISpatialReferenceFactory pS ...

  2. TIMAC 学习笔记(二)

    昨天大体上熟悉了TIMAC自带的CC2530的示范例程,今天先从演示抓包入手,分析四种不同的配置工程在空中传输的差异.随后,会按照扫描.组网.入网等MAC层接口函数入手,结合IEEE 802.15.4 ...

  3. 如何在Android SDK 下查看应用程序输出日志的方法

          该文章源于安卓教程网(http://android.662p.com),转载时要注明文章的来自和地址,感谢你的支持. 在Android程序中可以使用 android.util.Log 类来 ...

  4. IIS6,IIS7 最简单的重写URL

    虽然现在很少用IIS6,今天突然要把项目搬到老的服务器上(IIS6),对项目还要重新部署一下. 主要把时间花在了对url的重写上.其实很简单,如下: IIS6 网站 → 属性 → 主目录 → 配置 → ...

  5. 基于IOS和Android设备MDM技术方案服务价格

    导读:前段时间 www.mbaike.net 博客被恶意攻击,导致程序崩溃,目前已经替换了以前的Wordpress程序,现提供IOS和Android版本MDM的代码和相关文档咨询服务. 一.IOS版M ...

  6. 隐藏index.php - ThinkPHP完全开发手册 - 3.1

      为了更好的实现SEO优化,我们需要隐藏URL地址中的index.php,由于不同的服务器环境配置方法区别较大,apache环境下面的配置我们可以参考5.9 URL重写来实现,就不再多说了,这里大概 ...

  7. doctype的种类

    现实生活中人们使用的浏览器是各式各样的!为了防止各各浏览器页面设置不一样而照成我们所做的网页乱序,不得不设置doctye(文档类型):doctype有3中类型:* 过渡的(Transitional): ...

  8. coreseek安装使用

    本文引自:http://www.phperz.com/article/14/0615/95.html sphinx的安装使用,其实都是大同小异,以下以 coreseek安装为例Coreseek 是一款 ...

  9. asp.net runat="server" && hiddenfield

    runat="server", c#可以直接获得client控件,并且赋值 hiddenfield 可以作为传值,或者界面存值,后台每次读取,并且再赋值到前台,这样前台就可以把上一 ...

  10. background之你不知道的background-position

    这是一个有趣的话题 其实我并不确切的平时大家是怎么去应用或者玩转一个属性,一个值.我能肯定的是这些东西都有不少的可玩性. 我今天要聊的 background-position 应该已经被大家玩得色彩斑 ...