【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 ...
随机推荐
- U3D使anim,unity,prefab文件不显示乱码
Edit-Project Setting-Editor-Asset Serialization-mode Force Text
- iOS开发之生成二维码
一.二维码的生成 从iOS7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器 1.二维码的内容(传统的条形码只能放数字) 纯文本 名片 URL 2.生成二 ...
- 转义字符_MySQL识别下面的转义序列
转义字符.MySQL识别下面的转义序列 在字符串中,某些序列具有特殊含义.这些序列均用反斜线('\')开始,即所谓的转义字符.MySQL识别下面的转义序列: \0 ASCII 0(NUL)字符. \' ...
- 针对高通BMS的研究 高通电量计
点击打开链接 高通8064 8974 8926等pm芯片都集成了电量计,估计后续芯片都会一直存在,现在许多项目UI状态栏电池都有百分比显示,所以需要深入分析BMS有助于解决电量方面的BUG. 一: S ...
- uploadify springMVC
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 手机号码归属地查询api接口
淘宝网 API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON ...
- PHP : Reflection API
PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类.方法.属性.参数等的详细信息,包括注释. PHP Reflection API有: class Reflecti ...
- c++ 在windows下建立目录
#include <direct.h> #include <stdlib.h> #include <stdio.h> int main( void ) { ) { ...
- Gson手动序列化POJO(工具类)
gson2.7版本 只是简单的工具类(练习所用): package pojo; import javax.xml.bind.annotation.XmlSeeAlso; import com.goog ...
- no leveldbjni64-1.8 in java.library.path
在抽取以太坊Java版本的Trie树部分时,遇到了一个问题: Exception in thread "main" java.lang.UnsatisfiedLinkError: ...