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.

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 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:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
res = self.helper(root, sum)
return res def helper(self, root, sum):
if root is None:
return False
if root.left is None and root.right is None:
if sum == root.val:
return True
else:
return False
left = self.helper(root.left, sum - root.val)
right = self.helper(root.right, sum - root.val)
return left or right

[LC] 112. Path Sum的更多相关文章

  1. 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 ...

  2. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

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

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

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  6. [LeetCode] 112. Path Sum 路径和

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

  7. LeetCode OJ 112. Path Sum

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

  8. Leetcode 112. Path Sum

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

  9. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

随机推荐

  1. Java 类提供了自定义的构造方法,那么类的默认构造不会被调用

    以下代码无法通过编译: public class Test1 { public static void main(String[] args) { //int a=6; Foo obj=new Foo ...

  2. virtualbox 共享文件夹 操作过程

    1.在本地主机上找到 VBoxGuestAdditions.iso,我的位置就在E:\Oracle\VirtualBox\VBoxGuestAdditions.iso 2 复制到虚拟机中,我用的是 x ...

  3. post表单、json接口

    package com.lv.qggz.man.dhht.api.typesetting; import com.lv.qggz.man.dhht.api.typesetting.vo.UVO;imp ...

  4. 20个GitHub最热门的Java开源项目:文档、框架、工具

    专注于Java领域优质技术,欢迎关注 文章来源:JavaGuide 以下涉及到的数据统计,数据来源:https://github.com/trending/java?since=monthly[1] ...

  5. python对数组缺失值进行填充

    1. 两个常用的函数 1.1 np.nonzero() np.nonzero()函数返回数组中不为False(0)的元素对应的索引 a = np.array([1,2,0,3,1,0]) print( ...

  6. Kafka学习(学习过程记录)

    Apache kafka 这,仅是我学习过程中记录的笔记.确定了一个待研究的主题,对这个主题进行全方面的剖析.笔记是用来方便我回顾与学习的,欢迎大家与我进行交流沟通,共同成长.不止是技术. Kafka ...

  7. iTOP-4418开发板TF卡烧写-引导uboot

    基于迅为iTOP-4418开发板 将 TF 卡接入开发板,将拨码开关设置为 TF 卡启动,进入 uboot 模式,如下图所示. 如下图所示,使用命令“fastboot”,接着就可以通过 OTG 给 e ...

  8. 微信支付第三方sdk使用

    1.引入依赖:(对于依赖冲突自行解决) <dependency> <groupId>com.github.binarywang</groupId> <arti ...

  9. UML-类图-需要写关联名称吗?

    概念模型:需要写关联名称:类图:不需要写关联名称. 注意,概念模型关联线不需要箭头.

  10. h5 移动端在阻止touchstart的默认事件时报错

    h5 移动端在阻止touchstart的默认事件时报错 解决办法, 可以添加 *{ touch-action: none;}即可消除错误