原题地址:https://oj.leetcode.com/problems/path-sum/

题意:

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

解题思路:使用递归解决。

代码:

# 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 hasPathSum(self, root, sum):
if root == None:
return False
if root.left == None and root.right == None:
return root.val == sum
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

[leetcode]Path Sum @ Python的更多相关文章

  1. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  2. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [LeetCode] 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 ...

  5. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  7. LeetCode Path Sum IV

    原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...

  8. [leetcode]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 ...

  9. LeetCode: 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 ...

随机推荐

  1. bzoj3628: [JLOI2014]天天酷跑

    题目链接 bzoj3628: [JLOI2014]天天酷跑 题解 开始读错题目了,尴尬 由于题目说的跳跃次数和高度是在一开始设定的. 发现枚举一下记忆化搜索就可以过了 要注意,跳到最高点是可以不下坠继 ...

  2. JNI之String类型

    JNI使用的是改良的UTF-8格式的Strings. 以下文档来自官方: Modified UTF-8 Strings The JNI uses modified UTF-8 strings to r ...

  3. 精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过

    精益软件研发的秘密 IT大咖说 - 大咖干货,不再错过   http://www.itdks.com/dakashuo/new/dakalive/detail/3662

  4. JTAG - Debug Cable Driver/Receiver

  5. 爬虫IP被禁的简单解决方法

    爬虫以前听上去好厉害好神秘的样子,用好了可以成就像Google.百度这样的索索引擎,用不好可以凭借不恰当的高并发分分钟崩掉一个小型网站.写到这里想到12306每年扛住的并发请求量,觉得好牛逼. 爬虫和 ...

  6. 【Go入门教程3】基本类型 和 高级类型

    基本类型 Go 有很多预定义类型,这里简单地把它们分为 基本类型 和 高级类型.Go 的基本类型并不多,而且大部分都与整数相关,如下表所示: 名 称 宽度(字节) 零 值 说 明 bool 1 fal ...

  7. 关于pcie的备忘

    总线驱动:深度优先统计资源,深度滞后分配资源 资源包括Bus id和内存(prefectable和non-prefectable内存) 设备驱动:包括设备驱动层和消息通信 主要是四个部分: (1)中断 ...

  8. iOS 线程之GCD的高级使用方法

    之前的一篇关于线程的blog已经为大家介绍了GCD的简单使用方式及样例说明,今天因为项目中有特殊的应用GCD的实例,为大家介绍两种特殊需求的使用GCD的方法. 目的:实现一件事情做完,再做下一件事情. ...

  9. X.509 数字证书结构和实例

    http://www.cppblog.com/sleepwom/archive/2010/07/08/119746.html 一. X.509数字证书的编码 X.509证书的结构是用ASN1(Abst ...

  10. 【Devops】【docker】【CI/CD】2.docker启动jenkins环境+安装必要的插件

    [注意:]jenkins的docker镜像,需要从官网进入直接获取,其他地方获取到的docker镜像,可能因为Jenkins版本过低,导致后续插件安装失败等问题!!! ================ ...