Recursive Python without DFS and subtration from sum to zero


  • 0
    R
    class Solution(object):
        def hasPathSum(self, root, sum):
            self.summe = 0
            def preO(node):
                if node is None: return False
                else:
                    self.summe += node.val
                    if self.summe == sum and (node.left is None and node.right is None):
                        return True
                    else:
                        a = preO(node.left) or preO(node.right)
                        if not a:
                            self.summe -= node.val
                        return a
            return preO(root)
            ```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.