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. python刷LeetCode:7. 整数反转

    难度等级:简单 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 3: ...

  2. Baes.css

    这个公共样式还不错 html,body,ul,li,ol,img,a,p,div,form,input,h1,h2,h3,h4.h5,h6{ margin: 0; padding: 0; /*盒模型* ...

  3. 浅谈Redis五个对象类型的底层原理

    本博客强烈推荐: Java电子书高清PDF集合免费下载 https://www.cnblogs.com/yuxiang1/p/12099324.html Redis是一种key/value型数据库,其 ...

  4. excel 导出长数据 变成科学计数 解决办法

    加 “\t”

  5. 吴裕雄--天生自然 JAVA开发学习:Character 类

    char ch = 'a'; // Unicode 字符表示形式 char uniChar = '\u039A'; // 字符数组 char[] charArray ={ 'a', 'b', 'c', ...

  6. Python中列表的copy方法

    1.在列表中存在一个名为copy的方法,就像字面意思一样copy方法是用于复制列表元素的,示例如下: names = [‘Zhangsan’,’Lisi’,’WangErgou’] names2 = ...

  7. DNS服务器搭建与配置

    DNS服务器搭建与配置目录 1.DNS查询方式 2.DNS服务器类型 3.DNS主要配置文件组 4.name.conf文件配置介绍 5.DNS的资源记录格式 6.DNS服务器和客户端配置 7.简单搭建 ...

  8. flask框架-大结局

    flask-script 用于实现类似于django中 python3 manage.py runserver ...类似的命令. 安装 pip3 install flask-script 使用: f ...

  9. 洛谷 P5018 对称二叉树

    题目传送门 解题思路: 先计算每个点的子树有多少节点,然后判断每个子树是不是对称的,更新答案. AC代码: #include<iostream> #include<cstdio> ...

  10. Python的IDE之Pycharm的使用

    Python的IDE之Pycharm的使用 一.为什么用IDE(Python集成开发环境-Pycharm) 到现在为止,我们也是写过代码的人啦,但你有没有发现,每次写代码要新建文件.写完保存时还要选择 ...