【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 ...
随机推荐
- noi 8462 大盗阿福
题目链接:http://noi.openjudge.cn/ch0206/8462/ 相邻的两个不能同时取, d[i] = max(d[i-1],d[i-2]+a[i]); http://paste.u ...
- CentOS 7下关于systemd的一些唠叨话二:systemd服务脚本的编写
CentOS 7继承了RHEL 7的新的特性,例如强大的systemd,而systemd的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变,也大幅提高了系统服务的运行效率.但服务 ...
- 我的android学习经历39
关于像第一次进qq一样的那个渐变企鹅图的制作 渐变动画 package com.moonweather.app.activity; import com.moonweather.app.R; impo ...
- Java中request请求之 - 带文件上传的form表单
常用系统开发中总免不了显示图片,保存一些文件资料等操作. 这些操作的背后,就是程序员最熟悉的 enctype="multipart/form-data"类型的表单. 说起file类 ...
- 文件上传和下载(可批量上传)——Spring(三)
在文件上传和下载(可批量上传)——Spring(二)的基础上,发现了文件下载时,只有在Chrome浏览器下文件名正常显示,还有发布到服务器后,不能上传到指定的文件夹目录,如上传20160310.txt ...
- Linux下内存占用和CPU占用的计算
->使用free命令查看内存使用情况: 1.echo 3 > /proc/sys/vm/drop_caches 2.free 或者使用cat /proc/yourpid/status 来查 ...
- ios设备相关
设备方向 typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { UIInterfaceOrientationMaskPortrait ...
- 学习c++
慢慢的滑向无边无际的没有回头路的程序猿道路.坚持就是胜利. 致渣渣
- 《BI那点儿事》SQL Server 2008体系架构
Microsoft SQL Server是一个提供了联机事务处理.数据仓库.电子商务应用的数据库和数据分析的平台.体系架构是描述系统组成要素和要素之间关系的方式.Microsoft SQL Serve ...
- Html5 Canvas核心技术(图形,动画,游戏开发)--基础知识
基础知识 canvas 元素可以说是HTML5元素中最强大的一个,他真正的能力是通过canvas的context对象表现出来的.该环境对象可以从canvas元素身上获得. <body> & ...