题目:

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. 洛谷 P1890 gcd区间

    P1890 gcd区间 题目提供者 洛谷OnlineJudge 标签 数论(数学相关) 难度 普及/提高- 题目描述 给定一行n个正整数a[1]..a[n]. m次询问,每次询问给定一个区间[L,R] ...

  2. 你早该这么玩Excel 读书笔记

    1. Excel用来分析数据,至少要有一份原始数据和对于的分类汇总数据,这两种数据在一项任务中,应该是存放在同一个Excel文档中的,在书籍中,把他们叫做源数据表和分类汇总表.用户输入源数据表,根据相 ...

  3. grails导入excel

    grails导入excel,意思是说从excel表中读取多条数据,批量写入数据库. 有2种方案,1是使用grails的excel插件,2是调用java代码使用POI等第三方java控件. 今天比较累, ...

  4. laravel 框架 开源的cms推荐

    laravel 框架写的开源的cms系统 TypiCMS系统 多语言和模块化的CMS Laravel 5.2框架 下载地址:https://github.com/TypiCMS/Base Bootst ...

  5. 写了个Linux包过滤防火墙

    花几天写了个so easy的Linux包过滤防火墙,估计实际意义不是很大.防火墙包括用户态执行程序和内核模块,内核模块完全可以用iptable代替.由于在编写的过程一开始写的是内核模块所以就直接用上来 ...

  6. JavaScript 高级程序设计 02-变量、数据类型

    一.JavaScript变量 1.变量的定义 在定义变量时,统一使用关键字var,后跟变量名(即标识符,如果不知道什么是标识符,可以到这去查看),如下 var message; //定义一个变量 注意 ...

  7. FPGA串口波特率简析

    以前用单片机,一直都是直接用就行,设置波特率时,直接写9600就行,一直没有仔细考虑过,今天打算用FPGA写个串口程序时才知道,原来根本就是没弄明白.一下是我的一些见解.如果诸位看官觉得不对,欢迎指正 ...

  8. 使用公钥登录SSL

    在本地生成密钥对 ssh-keygen -t rsa 如果不想设置密码,可以直接点击回车. 如果你想使用DSA可以用-t DSA替换. 确保远程计算机上用户目录下有.ssh目录 确保你的连接服务器上的 ...

  9. 实验五 Java网络编程及安全

    北京电子科技学院 实      验      报      告 课程:移动平台应用开发实践  班级:201592   姓名:曾俊宏  学号:20159210 成绩:___________  指导老师: ...

  10. IOS 数组分组 Grouped NSArray

    NSMutableSet *set=[NSMutableSet set]; [_list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BO ...