73. LeetCode 112. 路径总和

2019-02-22  本文已影响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)
           

暂略。

上一篇 下一篇

猜你喜欢

热点阅读