博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 437. Path Sum III
阅读量:4635 次
发布时间:2019-06-09

本文共 868 字,大约阅读时间需要 2 分钟。

进入pathSum函数后有两种情况,一种是包括root的值一种是不包括.

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def __init__(self):        self.ans=0            def pathSum(self, root, sum):        """        :type root: TreeNode        :type sum: int        :rtype: int        """        if not root:            return 0                self.pathSumWithNode(root,sum)        self.pathSum(root.left,sum)        self.pathSum(root.right,sum)                return self.ans    def pathSumWithNode(self,root,sum):        if not root:            return 0        if root.val==sum:            self.ans+=1        self.pathSumWithNode(root.left,sum-root.val)        self.pathSumWithNode(root.right,sum-root.val)

 

转载于:https://www.cnblogs.com/zywscq/p/10507595.html

你可能感兴趣的文章
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
【BZOJ】2120: 数颜色
查看>>
spring boot 文件上传工具类(bug 已修改)
查看>>
《机电传动控制》学习笔记03-1
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
金蝶K/3 BOS产品培训教案
查看>>
章三 链表
查看>>
react组件回顶部
查看>>
【LeetCode】Palindrome Partitioning 解题报告
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
从壹开始微服务 [ DDD ] 之一 ║ D3模式设计初探 与 我的计划书
查看>>
python 错误之SyntaxError: Missing parentheses in call to 'print'
查看>>
Windows Phone开发(16):样式和控件模板
查看>>
CSE 3100 Systems Programming
查看>>
洛谷 1604——B进制星球(高精度算法)
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
Linux Supervisor的安装与使用入门
查看>>