[抄题]:

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——函数的返回值 The return value of the function

    # 在函数体中,通过return关键词返回函数的内部数据给外部 """# 一.作用# return作用:1.结束函数:2.将函数的内部数据返回给外部 def fn(): ...

  2. linux scp传输文件命令

    scp  -r /opt/test root@192.168.2.105:/opt

  3. zabbix之 zabbix模板监控mysql

    zabbix中默认有mysql的监控模板.默认已经在zabbix2.2及以上的版本中.模板名称:Template App MySQL.如果没有则要去zabbix官方下载 url:https://zab ...

  4. delphi 下载

    最新(更多)内容,请到  http://www.cnblogs.com/key-ok/p/3465486.html  Borland Pascal v7.1 (13.89 Mb)  Delphi 1 ...

  5. ThreadLocal的学习

    一 用法ThreadLocal用于保存某个线程共享变量:对于同一个static ThreadLocal,不同线程只能从中get,set,remove自己的变量,而不会影响其他线程的变量.1.Threa ...

  6. 知识点:Java 内存模型完全解密

    Java虚拟机(JVM) 规范中定义了一种Java的内存模型,即Java Memoory Model(简称JMM),用来实现让Java程序在各个平台下都能达到一致的内存访问效果. JVM是整个虚拟机, ...

  7. ProxySQL Cluster的搭建

    环境: proxysql-1.4.10-1-centos7.x86_64 db210 192.168.99.210 老节点,已经做成mysql配置和读写分离设置db211 192.168.99.211 ...

  8. [java,2018-02-24] svn检出项目名称不正确

    ,今天从svn中检出项目时发现,检出项目的名称与实际的不相同,如下图: 这才想起来,当时创建项目时是随意起了个test的名称作为项目名,后来觉得能用,就在me中直接把项目名称改掉,提交到了svn.再从 ...

  9. Java中字段、属性、成员变量、局部变量、实例变量、静态变量、类变量、常量

    首先看个例子: package zm.demo; public class Demo { private int Id;//成员变量(字段).实例变量(表示该Id变量既属于成员变量又属于实例变量) p ...

  10. Openface 入门

    Openface 简单入门 背景 Openface是一个开源的人脸识别框架,同类软件产品还有 seetaface ,DeepID等,当然,如果算上商业的产品,那就更多了. Openface人脸比对结果 ...