# Time:  O(n)
# Space: O(h), h is height of binary tree
#
# Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
# 
# For example:
# Given the below binary tree and sum = 22,
#               5
#              / \
#             4   8
#            /   / \
#           11  13  4
#          /  \    / \
#         7    2  5   1
# return
# [
#    [5,4,11,2],
#    [5,8,4,5]
# ]


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

class Solution:
    # @param root, a tree node
    # @param sum, an integer
    # @return a list of lists of integers
    def pathSum(self, root, sum):
        return self.Help(root, sum, [], [])
        
    def Help(self, root, sum, cur, result):
        if root is None:
            return result
        if root.left == None and root.right == None and root.val == sum:
            result.append(cur + [root.val])
        cur.append(root.val)
        self.Help(root.left,  sum - root.val, cur, result)
        self.Help(root.right, sum - root.val, cur, result)
        cur.pop()
        return result

if __name__ == "__main__":
    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.left = TreeNode(5);root.right.right.right = TreeNode(1);
    print Solution().pathSum(root, 22)
