[抄题]:

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么添加元素:连arraylist都忘了我去

backtracing的add-dfs-remove也忘了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

嵌套类型的右边也是对应的:

List<List<Integer>> result = new ArrayList<List<Integer>>();

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

dfs中如果是累计求和,就要求有全局变量sum。所以不如curSum每次缩小,最后变成root.val。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

为了避免全局变量,所以不如curSum每次缩小,最后变成root.val。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
//initialization
List<Integer> cur = new ArrayList<>();
List<List<Integer>> result = new ArrayList<List<Integer>>(); //corner case
if (root == null) return result;
dfs(root, sum, cur, result);
return result;
} public void dfs(TreeNode root, int curSum, List<Integer> cur, List<List<Integer>> result) {
//corner case: root == null
if (root == null) return ; //add to result
cur.add(root.val); if (root.left == null && root.right == null && curSum == root.val)
result.add(new ArrayList(cur));
else {
//dfs
dfs(root.left, curSum - root.val, cur, result);
dfs(root.right, curSum - root.val, cur, result);
} //remove last
cur.remove(cur.size() - 1);
}
}

113. Path Sum II 输出每个具体路径的更多相关文章

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

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

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

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

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

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

  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 路径和 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 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

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

随机推荐

  1. PythonStudy——字符串常用操作 String common operations

    # 1.字符串的索引取值: 字符串[index]# 正向取值从0编号,反向取值从-1编号 s1 = '123abc呵呵' t_s = ' # 取出c print(s1[5], s1[-3]) # 2. ...

  2. c# winform窗体如何设置才可以不能随意拖动大小

    执行以下两个步骤,能够禁止用户改变窗体的大小 (一)步骤1 设置窗体的FormBorderStyle属性为下列五个值中的任意一个 None:将窗口设置为无边框.无标题栏.用户无法改变窗口的大小,也无法 ...

  3. 高性能网络编程之IO和NIO阻塞分析

    一.内容 1.阻塞和非阻塞是什么? 2.传统IO模型,他存在哪些阻塞点 3.NIO模型 4.对比总结 1.阻塞和非阻塞是什么? 阻塞:做某件事情,直到完成,除非超时,如果没有完成,继续等待. 非阻塞: ...

  4. cordova插件列表

    主要来源为http://blog.csdn.net/github_39500961/article/details/76270299 1.获取当前应用的版本号 cordova plugin add c ...

  5. for批处理skip参数不支持变量延迟!n!的解决办法

    a.txt 文件a第1行 文件a第2行 文件a第3行 b.txt 文件b第1行 文件b第2行 文件b第3行 合并ab .bat @echo off REM 把两个文件逐行合并成一列 set n=0 f ...

  6. kafka consumer防止数据丢失(转)

    http://kane-xie.iteye.com/blog/2225085 kafka最初是被LinkedIn设计用来处理log的分布式消息系统,因此它的着眼点不在数据的安全性(log偶尔丢几条无所 ...

  7. ps-如何去背景色(将背景色变透明)

    由于生活或工作的需求,图片的处理是必不可少.其中将图片某一部分变为透明,或者截取图片的某一部分比较常见. 1.首先,打开待处理的图片: 2.复制背景图层,将背景图层设为不可见(左边的眼睛即可),选择左 ...

  8. SAS LOGISTIC 逻辑回归中加(EVENT='1')和不加(EVENT='1')区别

    区别在于:最大似然估计分析中估计是刚好正负对调加上EVENT:%LET DVVAR = Y;%LET LOGIT_IN = S.T3;%LET LOGIT_MODEL = S.Model_Params ...

  9. Java访问Phoenix连接

    两种方法,一种是直接使用jdbc连接,一种是使用spring连接. jdbc连接和访问oracle步骤相同: ///////////// 测试Phoenix连接 /////////////// Str ...

  10. Keras处理已保存模型中的自定义层(或其他自定义对象)

    如果要加载的模型包含自定义层或其他自定义类或函数,则可以通过 custom_objects 参数将它们传递给加载机制: from keras.models import load_model # 假设 ...