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. 新浪SAE云平台下使用codeigniter的数据库配置

    新浪SAE云平台下使用codeigniter的数据库配置 投稿:shichen2014 字体:[增加 减小] 类型:转载 这篇文章主要介绍了新浪SAE云平台下使用codeigniter的数据库配置,主 ...

  2. Vue-router(5)之 路由的before家族

    beforeEach方法 import Vue from 'vue' import Router from 'vue-router' import Son1 from '@/view/New/son1 ...

  3. Python-django入门

    上网流程: Django的安装

  4. Maven--配置 Maven 从 Nexus 下载构件

    在 POM 中配置: <project> ... <repositories> <repository> <id>nexus</id> &l ...

  5. TF分布式问题

    碰到一个没解决的问题. 用tensorflow 分布式异步更新模式训练模型, 模型中带正则项, 每个batch的损失函数为 \[\lambda \|W\|_1 + \frac 1 {N_j} \sum ...

  6. flask框架-中

    路由扩展 @app.route和app.add_url_rule参数 # rule,URL 规则 # view_func,视图含数名称 # defaults = None,默认值,当url中无参数,函 ...

  7. mui + H5 调取摄像头和相册 实现图片上传

    最近要用MUI做项目,在研究图片上传时 ,遇到了大坑 ,网上搜集各种资料,最终写了一个demo,直接看代码.参考(http://www.cnblogs.com/richerdyoung/p/66123 ...

  8. cassandra 系统分析 架构

    cassandra cassandra是无中心节点的列式数据库 集群管理:      使用gossip算法,最终每个节点都知道集群中的所有节点信息,新增一个节点,新节点发送上线消息,     其他节点 ...

  9. windows下查看rabbitmq服务是否启动

    1.命令行进入rabbitmq的安装目录下: 如下图1步骤 2.输入命令 rabbitmqctl1 status 如下图2步骤 3.有时会提示报错,如步骤3 解决办法: 我的电脑 ==> 右键 ...

  10. 17.3.13---socket网络套接字介绍--函数和数据类型

    1----Socket类型 套接字格式:socket(family,type[,protocal])使用给定的地址族,套接字类型,协议编号(默认为0)来创建套接字 socket类型 描述 socket ...