Say I have a binary tree with the paths from root to leaf being 3-1-3, 3-4-5, 3-4-1 ... How would I go about returning a list of all the different paths? This is what I have so far, but all it does is return [[3, 1, 3, 4, 1, 5], [3, 1, 3, 4, 1, 5], [3, 1, 3, 4, 1, 5]], which is three lists of all the nodes in the tree instead of a separate list for each path.
self.res = []
def helper(root, temp):
if not root:
return
temp.append(root.val)
if not root.left and not root.right:
self.res.append(temp)
return
helper(root.left, temp)
helper(root.right, temp)
helper(root, [])