Wednesday, August 19, 2015

Leetcode 112. Path Sum

https://leetcode.com/problems/path-sum/

How to avoid too many branches?

Solution:
# T:O(n) S:O(h)
class Solution:
    # @param root, a tree node
    # @param sum, an integer
    # @return a boolean
    def hasPathSum(self, root, sum):
        if root is None: return False
        if root.left is None and root.right is None and root.val == sum:
            return True
        return self.hasPathSum(root.left, sum - root.val)\
      or self.hasPathSum(root.right, sum - root.val)
Run Time: 84 ms

No comments:

Post a Comment