[抄题]:

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. ANSYS耦合

    目录 定义 如何生成耦合自由度集 1.在给定节点处生成并修改耦合自由度集 2.耦合重合节点. 3.迫使节点有相同的表现方式 生成更多的耦合集 1. CPLGEN 2.CPSGEN 使用耦合注意事项 约 ...

  2. github 的ssh key

    一.输入cd ~/.ssh——回车(看你是否有了ssh key 密钥): 二.若无密匙,输入ssh-keygen -t rsa -C "your email"——直接回车,回车,跟 ...

  3. Spring Boot/Spring Cloud

    104.什么是 spring boot?         在Spring框架这个大家族中,产生了很多衍生框架,比如 Spring.SpringMvc框架等,Spring的核心内容在于控制反转(IOC) ...

  4. 深入Session2

    一.分布式环境Session的处理方法 分布式环境下要保持会话跟踪最简单的方式是只依靠客户端Cookie保存,不过大多数情况下还需要用到Session,一般的处理方式如下: 1.Session复制 每 ...

  5. 装系统时 System clock uses UTC 问题

    装系统也装了至少不下50次了,每次都是傻瓜一样的按照第一印象在弄,从未想过为啥,装到这里的时候,System clock uses UTC 勾不勾呢,每次都是百度,然后装完这一次下一次又忘了,这是没有 ...

  6. 移植Valgrind检测Android JNI内存泄漏

    1.相关工具 Valgrind:从Valgrind官网下载最新的源码包,我这里用的是:valgrind 3.14.0 (tar.bz2) [17MB] - 9 October 2018. Ubuntu ...

  7. IIS 7.0的集成模式和经典模式

    IIS7.0中的Web应用程序有两种配置模式:经典模式和集成模式.经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库, 原先运行于IIS6.0下的Web应用程序迁移到IIS7 ...

  8. Python撰写mail

    版本1   指定邮箱进行发送 """ 说明:指定账户密码进行邮件发送 由312051952@qq.com-->c4kaichen@163.com "&qu ...

  9. 刘志梅 201771010115 《面向对象程序设计(java)》 第八周学习总结

    实验六 接口的定义与使用 实验时间 2018-10-18 1.实验目的与要求 (1) 接口定义:接口不是类,而是对类的一组需求描述,这些类要遵从接口描述的统一格式进行定义:由常量和一组抽象方法组成:接 ...

  10. atnodes命令使用方法

    一条命令可以同时执行多台机器,结果会输出列表. atnodes -L 'grep -c "查询订单列表,userId=bing.wang03" /home/w/www/order- ...