"""Leetcode 112. Path Sum
Easy

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

Given a binary tree and a sum, determine if the tree has a root-to-leaf path
such that adding up all the values along the path equals the given sum.

Note: A leaf is a node with no children.

Example:
Given the below binary tree and sum = 22,
      5
     / \
    4   8
   /   / \
  11  13  4
 /  \      \
7    2      1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
"""

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


class SolutionPreorderRecur(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool

        Time complexity: O(n).
        Space complexity: O(logn) for balanced tree; O(n) for single sided.
        """
        # Apply recursive preorder traversal.
        if not root:
            return False

        if root.val == sum and not root.left and not root.right:
            return True

        return (self.hasPathSum(root.left, sum - root.val) or
                self.hasPathSum(root.right, sum - root.val))


class SolutionPreorderIter(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool

        Time complexity: O(n).
        Space complexity: O(logn) for balanced tree; O(n) for single sided.
        """
        # Apply iterative preorder traversal with queue.
        if not root:
            return False

        # Apply DFS with stack.
        stack = [(root, sum)]

        while stack:
            cur, _sum = stack.pop()

            if cur.val == _sum and not cur.left and not cur.right:
                return True

            # If exist, append right before left, since we use stack.
            if cur.right:
                stack.append((cur.right, _sum - cur.val))
            if cur.left:
                stack.append((cur.left, _sum - cur.val))

        return False


def main():
    # Given the below binary tree and sum = 22,
    #       5
    #      / \
    #     4   8
    #    /   / \
    #   11  13  4
    #  /  \      \
    # 7    2      1
    # return true, 
    root = TreeNode(5)
    root.left = TreeNode(4)
    root.right = TreeNode(8)
    root.left.left = TreeNode(11)
    root.right.left = TreeNode(13)
    root.right.right = TreeNode(4)
    root.left.left.left = TreeNode(7)
    root.left.left.right = TreeNode(2)
    root.right.right.right = TreeNode(1)
    sum = 22
    print SolutionPreorderRecur().hasPathSum(root, sum)
    print SolutionPreorderIter().hasPathSum(root, sum)


if __name__ == '__main__':
    main()

@@ -1707,16 +1707,20 @@
      cur
+rent
 , _sum =
@@ -1747,24 +1747,28 @@
       if cur
+rent
 .val == _sum
@@ -1775,24 +1775,28 @@
  and not cur
+rent
 .left and no
@@ -1796,24 +1796,28 @@
  and not cur
+rent
 .right:%0A    
@@ -1929,16 +1929,20 @@
   if cur
+rent
 .right:%0A
@@ -1974,16 +1974,20 @@
 end((cur
+rent
 .right, 
@@ -1992,24 +1992,28 @@
 , _sum - cur
+rent
 .val))%0A     
@@ -2025,16 +2025,20 @@
   if cur
+rent
 .left:%0A 
@@ -2069,16 +2069,20 @@
 end((cur
+rent
 .left, _
@@ -2090,16 +2090,20 @@
 um - cur
+rent
 .val))%0A%0A

Revise cur to current for clarification