[抄题]:

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. Netty学习记录

    一.Netty简介 Netty 是一个基于 JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性. Netty 是一个 NIO client-s ...

  2. “数据上帝” Jeff Hammerbacher

    出生于1983年的数学天才Jeff Hammerbacher在23岁时加入了Facebook,一手组建起数据分析队伍.他是“数据科学”(data science)一词的提出者之一,被人们称为“数据上帝 ...

  3. linux(centos6.9)安装步骤

    http://blog.csdn.net/u012453843/article/details/52819756 https://mirrors.aliyun.com/ 下载想要的版本 2选择新建虚拟 ...

  4. 1.1.10 word2010图片另存为存到图片库里,怎么修改之

    1. 打开一个文档,在图片上右击,图片另存为,在弹出的[保存文件]对话框中. 2.单击左上角的[组织]下拉列表"属性"按钮,在弹出的[图片 属性]对话框中,单击[包含文件夹]按钮, ...

  5. python运行过程

    程序执行过程 PyCodeObject:PyCodeObject则是Python编译器真正编译成的结果. 当python程序运行时,编译的结果则是保存在位于内存中的PyCodeObject中,当Pyt ...

  6. python永久添加环境变量

    import sys sys.path 系统环境是一个list,可以将自己需要的库添加进入,例如mysql库,hive库等等.有三种方式添加,均验证通过:     1 临时添加,在一个shell窗口中 ...

  7. ubuntu server cloud img username password

    新安装了OpenStack Queens发现无镜像,蹦蹦跳跳的下载了ubuntu的镜像 网址https://cloud-images.ubuntu.com/ 最好你自己找你想要的,vmdk.ova.i ...

  8. 四、Html列表、块、布局

  9. [Oracle,2018-03-02] oracle一次插入多条记录

    insert into student(name,age) ' from dual union all ' from dual union all ' from dual 在oracle中不能像mys ...

  10. nginx ------反向代理和负载均衡

    最近由于公司的业务增长 服务器承受不住压力经常出现崩溃现象 为了解决 使用nginx的负载均衡解决,以下是操作步骤: 1.nginx 的负载均衡:将压力分散到不同的机器上 nginx不单可以作为强大的 ...