[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 ...
随机推荐
- 使用maven打包问题
项目打包:选择项目 右键->run as-> maven install . 项目中使用的是maven项目,将项目打包成war的时候有时候会出现 出现这种情况的时候解决步骤如下: 选择要打 ...
- POJ 3659 再谈树形DP
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5325 Accepted: 188 ...
- (2)关于opencv解压
关于opencv解压,一定不能解压到你的C盘的 ProgramFile(x86)中,不然,你肯定不会成功,你要放在C盘的其他文件夹,或者是别的盘中 就是因为这一个错误,我弄了一天,哎哎,时间宝贵啊
- Centos配置NAT模式下的静态ip
一.查看所在的ip段 点击 编辑-->虚拟网卡编辑器 选中vmware8网卡,点击 DHCP设置 二.编辑网卡配置文件 查看网卡 ip addr 命令打开配置文件 vi /etc/sysconf ...
- goweb-模板引擎
模板引擎 Go 为我们提供了 text/template 库和 html/template 库这两个模板引擎,模板引 擎通过将数据和模板组合在一起生成最终的 HTML,而处理器负责调用模板引擎并将引 ...
- Evaluation metrics for classification
Accuracy/Error rate ACC = (TP+TN)/(P+N) ERR = (FP+FN)/(P+N) = 1-ACC Confusion matrix Precision/Recal ...
- python中的倒序遍历
1.在列表本身倒序 a = [1, 3, 7, 5, 2, 6] a.reverse() # 在列表本身进行倒序,不返回新的值 print(a) # 输出a: # [6, 2, 5, 7, 3, 1] ...
- 编程基础-servlet1
1.Servelet是什么 sevlet是Server与Applet 的缩写,即服务端小程序.Sun公司提供的开发动态web资源的技术. servelet本质是java类,但遵循Servlet规范,没 ...
- SLAM资料
当下SLAM方案的总体介绍 http://wwwbuild.net/roboteasy/908066.html slam基础知识 https://www.zhihu.com/question/3518 ...
- look and say 外观数列的python实现
#look_and_say 外观数列 如果我们把 1 作为Look-and-say 数列的第一项,那么,它的前几项是这样的: 1, 11, 21, 1211, 111221, 312211, 1311 ...