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]
] Time: O(N)
Space: O(Height)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
res, lst = [], []
self.helper(root, sum, res, lst)
return res def helper(self, root, sum, res, lst):
if root is None:
return
if root.left is None and root.right is None:
if sum == root.val:
lst.append(root.val)
res.append(list(lst))
lst.pop()
return
lst.append(root.val)
left = self.helper(root.left, sum - root.val, res, lst)
right = self.helper(root.right, sum - root.val, res, lst)
lst.pop()

[LC] 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 ...

随机推荐

  1. Redis主从复制以及主从复制原理

    Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的 API.从 2010年 3 月 15 日起,Redis 的开 ...

  2. SQL server 查询常用语句 2019.3.20

    SQL查询语句 select ...列名 from 表名 投影查询 select sno num,2019-sage as birthday // 给列起别名 from student: 在每个学生姓 ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. libcurl在windows下的使用

    curl在linux下很好用,但到了windows下写程序却没办法使用了,这时候可以使用libcurl库 libcurl库的编译网上很多,我就不一一赘述了,curl的官方网站:https://curl ...

  5. Digit sum (第 44 届 ACM/ICPC 亚洲区域赛(上海)网络赛)进制预处理水题

    131072K   A digit sum S_b(n)Sb​(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 ...

  6. git commit 后 尚未push到远程,撤销commit

    执行commit后,还没执行push时,想要撤销这次的commit,该怎么办? 解决方案: 使用命令: git reset --soft HEAD^ 这样就成功撤销了commit,如果想要连着add也 ...

  7. idea创建远程分支

    1.先从远程拉取一个完整的分支,master或dev 2.新建一个分支,new,新建的同时checkout 出来 3.把新建的分支push到远程 4.如果新建完分支后写代码了,需要先把代码提交然后一起 ...

  8. linux tar/ tar.gz文件解压

    1.tar 压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg   //将目录里所有jpg文件打包成 ...

  9. caffe-fasterrcnn程序理解

    faster-rcnn 结构杂谈  参考博客:::https://www.cnblogs.com/longriyao/p/5832274.html http://www.cnblogs.com/cha ...

  10. openlayers的loaders方式加载

    openlayers loaders方式加载 let layerVector = new ol.layer.Vector({ source : new ol.source.Vector({ loade ...