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. PL/SQL 连接oracle步骤

    下面就将PL/SQL的配置说明一下. 一.安装Oracle客户端,让后配置    安装目录下面的C:\ORACLE\instantclient_11_2\NETWORK\ADMIN 的 tnsname ...

  2. 教你如何使用JavaScript入门

    JavaScript简介   JavaScript是NetScape公司为Navigator浏览器开发的,是web前端卸载HTML文件中的一种脚本语言,能实现网页内容的交互显示.当用户在客户端显示该网 ...

  3. Idea Spring工程不识别注解

    如图所示 Idea工具报出很多注解不识别,开始怀疑是 工具问题,重装Idea.配置lombak都不行,切换分支发现正常,一定是合入代码修改啥了,一行行比对,果然是这行 import org.sprin ...

  4. 内存管理之堆heap

    1.什么是堆? 堆(heap)是一种内存管理方式.内存管理对操作系统来说是一件非常复杂的事情,因为首先内存容量很大, 其次就是内存需求在时间和大小块上没有规律(操作系统上运行着几十甚至几百个进程,这些 ...

  5. Centos配置NAT模式下的静态ip

    一.查看所在的ip段 点击 编辑-->虚拟网卡编辑器 选中vmware8网卡,点击 DHCP设置 二.编辑网卡配置文件 查看网卡 ip addr 命令打开配置文件 vi /etc/sysconf ...

  6. softmax和分类模型

    softmax和分类模型 内容包含: softmax回归的基本概念 如何获取Fashion-MNIST数据集和读取数据 softmax回归模型的从零开始实现,实现一个对Fashion-MNIST训练集 ...

  7. linux的centos设置静态网络

    这个是该自己的网络排至,具体的分析,自己以后再研究 http://www.centoscn.com/CentOS/config/2015/0227/4753.html

  8. linux系统终端介绍

    https://zhidao.baidu.com/question/174261014.html

  9. Go-语言基础-变量-类型-函数

    第一个程序 //单行注释 /* 多行注释 */ package main // 表示当前go文件属于main包 import "fmt" // 导入包 //编译型语言需要有一个入口 ...

  10. Vue专题-js常用指令

    vue.js官方给自己的定为是数据模板引擎,并给出了一套渲染数据的指令.本文详细介绍了vue.js的常用指令. vue.js常用指令 Vue.js使用方式及文本插值 Vue.js 使用了基于 HTML ...