美文网首页
73. LeetCode 112. 路径总和

73. LeetCode 112. 路径总和

作者: 月牙眼的楼下小黑 | 来源:发表于2019-02-22 16:04 被阅读7次
  • 标签:
  • 难度: 简单

  • 题目描述
  • 我的解法

注意递归出口设计, easy~


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        if root == None:
            return False
        if not root.left and not root.right:
            if root.val != sum:
                return False
            else:
                return True 
        return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
           
  • 其他解法

暂略。

相关文章

网友评论

      本文标题:73. LeetCode 112. 路径总和

      本文链接:https://www.haomeiwen.com/subject/zlmeyqtx.html