1、类中递归调用函数需要加self

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

leetcode:Path Sum【Python版】的更多相关文章

  1. [leetcode]Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the ...

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

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

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

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

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

  5. [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 ...

  6. [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 ...

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

  8. LeetCode Path Sum IV

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

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

  10. 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. (GoRails) 如何去掉form输入框头尾的空格;何时用callbacks,gem;

    视频:https://gorails.com/episodes/when-callbacks-and-adding-dependencies-are-good?autoplay=1 主题:应当在什么时 ...

  2. Confluence 6 使用 LDAP 授权连接一个内部目录 - 服务器设置

    名字(Name) 名字的描述将会帮助你在目录中识别.例如: Internal directory with LDAP Authentication Corporate LDAP for Authent ...

  3. HDU-1272 小希的迷宫 (并查集、判断图是否为树)

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

  4. dp入门:最长不下降序列

    #include "bits/stdc++.h" using namespace std; ],dp[]; int main() { int n; cin >> n; ...

  5. 使用Windbg解析dump文件

    WinDbg OllyDbg SoftICE (已经停止更新) 虽说WinDbg在无源码调试方面确实比较困难,但在调试内核方面却真的有独到之处. https://www.pediy.com/kssd/ ...

  6. laravel通过Eloquent ORM实现CURD

    //Eloquent ORM public function orm1() { //all(); 返回所有数据: /*$students=Student::all(); dd($students);* ...

  7. PHP:第三章——数组中的array_values

    例: <?php header("Content-Type:text/html;charset=utf-8"); //array_value(); //功能:返回数组中所有的 ...

  8. SPOJ AMR10I 递归

    DES :给你n 块石头.不会超过70.把它们分成n堆.每堆里的石头数做积.问共有多少个数.最终的结果除了1之外都能分解成素数相乘或者素数相乘再乘1.所以可以找到所有不超过70的素数然后进行深搜. 感 ...

  9. harbor私有镜像仓库的搭建与使用与主从复制

    harbor私有镜像仓库,私有仓库有两种,一种是harbor,一种是小型的私有仓库,harbor有两种模式,一种是主 从,一种是高可用仓库,项目需求,需要两台服务器,都有docker.ldap权限统一 ...

  10. Fedora 安装与常用命令

    fedora下载地址: https://getfedora.org/ 原来装的26,现在装27 用的是server版网络安装500多M 现在都是U盘安装了,fedora的写优盘说明 https://f ...