【LeetCode OJ】Path Sum II
Problem Link:
http://oj.leetcode.com/problems/path-sum-ii/
The basic idea here is same to that of Path Sum. However, since the problem is asking for all possible root-to-leaf paths, so we should use BFS but not DFS.
The python code is as follows.
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @param sum, an integer
# @return a boolean
def pathSum(self, root, sum):
"""
BFS from the root to leaves.
For each leaf, check the path sum with the target sum
"""
if not root:
return []
q = [([root],0)]
res = []
while q:
new_q = []
for path, s in q:
n = path[-1]
s += n.val
# If path is a root-to-leaf path
if n.left == n.right == None:
if sum == s:
res.append([p.val for p in path])
else: # Continue BFS
if n.left:
new_q.append((path+[n.left], s))
if n.right:
new_q.append((path+[n.right],s))
q = new_q
return res
【LeetCode OJ】Path Sum II的更多相关文章
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- LeetCode OJ 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 OJ: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 ...
- 【Leetcode 167】Two Sum II - Input array is sorted
问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...
- 【LeetCode OJ】Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number. T ...
随机推荐
- 添加一个DataSet
/// <summary> /// 返回状态数据 /// </summary> /// <param name="values"></pa ...
- Windows下Eclipse连接hadoop
2015-3-27 参考: http://www.cnblogs.com/baixl/p/4154429.html http://blog.csdn.net/u010911997/article/de ...
- DataGridView单元格美化
#region 重绘Column.Row int _RowHeadWidth = 41; /// /// 重绘Column.Row ...
- 大分享-hibernate,springmvc,easyui简要介绍
近期公司一直在做项目,主要用到了springMVC,eseayui,hibernate几大框架.近一个月的时间,个人就目前自我知识给予分享. 很多公司使用mybatis产品,综合所述其最大优点是全SQ ...
- 关于Youtube URL的十个技巧
你一定很熟悉Youtube了,知道它是一个视频分享网站.是的,youtube目前十分流行,你也许会常常访问.这里有一些关于youtube url的技巧,了解了这些技巧,你就可以更好的利用youtube ...
- Oracle使用经验总结
oracle数据库是一种大型数据库系统,一般应用于商业,政府部门,它的功能很强大,能够处理大批量的数据,在网络方面也用的非常多.Oracle数据库管理系统是一个以关系型和面向对象为中心管理数据的数据库 ...
- python去除空格和换行符的方法
一.去除空格 strip() " xyz ".strip() # returns "xyz" " xyz ".lstrip() # retu ...
- nginx 缓存机制
nginx 缓存机制 Nginx缓存的基本思路 利用请求的局部性原理,将请求过的内容在本地建立一个副本,下次访问时不再连接到后端服务器,直接响应本地内容 Nginx服务器启动后,会对本地磁盘上的缓 ...
- 简单回忆一下JavaScript中的数据类型
说到JavaScript,大家都应该知道,它是一门脚本语言,也是一门弱类型语言,也是一门解析型的语言,同时也是一门动态类型的语言. 很好,至于JavaScript中数据类型.其分为基本数据类型和复杂数 ...
- java读取配置文件中数据
Properties pps=new Properties(); try { pps.load(new FileInputStream("src/emai ...