码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 112 Path Sum

时间:2019-03-10 09:49:03      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:tree node   path   ret   tco   ==   int   color   obj   solution   

简单递归.

# 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 not root:
            return False
        if (not root.left) & (not root.right):
            return root.val==sum
        elif not root.left:
            return self.hasPathSum(root.right,sum-root.val)
        elif not root.right:
            return self.hasPathSum(root.left,sum-root.val)
        else:
            return (self.hasPathSum(root.left,sum-root.val))|(self.hasPathSum(root.right,sum-root.val))

 

Leetcode 112 Path Sum

标签:tree node   path   ret   tco   ==   int   color   obj   solution   

原文地址:https://www.cnblogs.com/zywscq/p/10504059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!