"""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

        # Base case: root-to-leaf path sum is sum.
        if root.val == sum and not root.left and not root.right:
            return True

        # If not, traverse left and right nodes.
        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:
            current, _sum = stack.pop()

            # Base case: root-to-leaf path sum is sum.
            if current.val == _sum and not current.left and not current.right:
                return True

            # Append right before left to stack.
            if current.right:
                stack.append((current.right, _sum - current.val))
            if current.left:
                stack.append((current.left, _sum - current.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()

@@ -1596,32 +1596,71 @@
 ed.%0A        %22%22%22%0A
+        from collections import deque%0A%0A
         # Apply 
@@ -1795,16 +1795,22 @@
 stack = 
+deque(
 %5B(root, 
@@ -1814,16 +1814,17 @@
 t, sum)%5D
+)
 %0A%0A      
@@ -2067,34 +2067,33 @@
 end 
-right before left to stack
+left and right if existed
 .%0A  
@@ -2105,36 +2105,35 @@
      if current.
-righ
+lef
 t:%0A             
@@ -2139,32 +2139,36 @@
     stack.append
+left
 ((current.right,
@@ -2161,20 +2161,19 @@
 current.
-righ
+lef
 t, _sum 
@@ -2203,35 +2203,36 @@
      if current.
-lef
+righ
 t:%0A             
@@ -2246,16 +2246,20 @@
 k.append
+left
 ((curren
@@ -2252,35 +2252,36 @@
 ndleft((current.
-lef
+righ
 t, _sum - curren

Refactor by using deque