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 关于md5加密

    package com.mi.util; /** * md5+salt 长度为32的加密 * @author admin * */ public class MD5 { public static v ...

  2. paddle(一)

    一.概述 一个机器学习的框架,提供了深度学习需要的神经网络,激活函数等主要功能. 基础概念 Program 一次模型训练就是一个program,通过执行器执行,默认环境下是执行fluid.defaul ...

  3. Python笔记_第四篇_高阶编程_检测_1.对函数进行单元检测

    1. 对函数进行单元检测: 单元检测: 作用:用来对一个函数.一个类.一个模块进行正确性校验工作. 结果: * 单元测试通过,说明我们测试函数的功能正确. * 单元测试不通过,说明函数有BUG,要么测 ...

  4. UML类图说明

    1:示例 这是一个使用UML表示的类图的结构,通过箭头,菱形,实线以及虚线来代表一些类之间的关系,后面将按照上面的例子一一介绍说明. 上图中,abstract 车是一个抽象类.小汽车和自行车是继承了车 ...

  5. leetcode中的sql

    1 组合两张表 组合两张表, 题目很简单, 主要考察JOIN语法的使用.唯一需要注意的一点, 是题目中的这句话, "无论 person 是否有地址信息".说明即使Person表, ...

  6. 1)基本的MFC程序创建过程

    1)基本的MFC创建过程: 2)   选择MFC应用程序: 3)然后选择特定的选项  直接完成就行了: 4)下面就是建成的样子: 5)然后是  运行结果: 6)有一个问题  那个  菜单栏是属于  F ...

  7. Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency

    Error : Execution failed for task ’ :app: preDebugAndroidTestBuild’.Conflict with dependency ‘com.an ...

  8. ZJNU 1205 - 侦探推理——高级

    双层枚举嫌疑犯与当日是星期几,统计真话与假话是否满足题意 注意 fake<=N&&fake+neutral>=N 即假话数量不大于N,假话加上没用的废话数量不小于N (注意 ...

  9. MySql索引机制

    第一部分 MySQL数据库索引的数据结构及算法理论 第二部分 MySQL索引实现机制 第三部分 MySQL中高性能使用索引的策略 数据结构及算法 MySQL官方对索引的定义为:索引(Index)是帮助 ...

  10. 微信小程序java8 java7 java6 encryptedData 解密 异常处理

    使用java8 java7  java6 解密微信小程序encryptedData可以回遇到一些错误 1.java.security.NoSuchAlgorithmException: Cannot ...