[LC] 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 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的更多相关文章
- 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 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 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 ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 【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 ...
- [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 ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
- 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 ...
随机推荐
- 用户体验评价——win10自带微软拼音输入法
目前正在使用的一款输入法就是win10自带的微软拼音输入法, 用户界面,一直在用个人感觉最大的特点就是十分简洁,界面的布局十分清晰,可以隐藏悬浮窗让他显示在菜单栏, 另外其记住用户输入习惯方面也十分优 ...
- NABCD模型——星遇
我们项目是个面向希望有新奇体验的用户的社交软件,致力于打造不一样的有趣的社交. 发表后一周预计用户量:1000人 N:(Need,需求) 目前主流社交软件由于时间原因体量越来越大,各种繁琐而不必要的功 ...
- Red Hat、Fedora和Ubuntu软件包操作
五.Red Hat.Fedora和Ubuntu软件包操作对比 最后总结: 在 /etc/yum.conf 的 [main] 后面添加以下配置即可! 复制代码 代码如下: exclude=kernel* ...
- 在storyboard中给控制器添加导航栏控制器和标签控制器
1.选中目标控制器 2.选择xcode的工具栏中的"Editor"->"Embed in"->"Navigation Controller ...
- ubuntu14.10安装gitlab
1 换源: # curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/ ...
- python计算范数的问题
计算norm时, 注意把数据改为float 类型, 否则结果不对! import numpy.linalg as LA d = LA.svd(features.toarray(),full_matri ...
- CodeForces - 1243D. 0-1 MST(补图连通分量个数)
Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: ...
- UML-领域模型-如何找到概念类
有3个方法 方法1:对已有的重用和修改(这是最好的方法) 重用和修改现有模型.这些模型来源于之前的项目.网上的 方法2:使用分类列表 从网上搜索该领域的常见分类,或参考书籍Martin Fowler的 ...
- Tkinter控件Canvas
网上关于tkinter的canvas组件系统的中文教程很少,英文教程未知.要么是专业的参考文档,没有丰富的实例,要么在不同的论坛,博客平台零零散散存在一些canvas的例子,这给学习canvas带来了 ...
- Django-rest framework框架的三大认证组件
源码分析:三大认证组件的封装 组件的认证配置: 模型层:models.py class User(BaseModel): username = models.CharField(verbose_nam ...