Skip to content

Commit

Permalink
Create binary-tree-paths.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Aug 16, 2015
1 parent 74f02df commit 8f03873
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Python/binary-tree-paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Time: O(n * h)
# Space: O(h)
#
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
result, path = [], []
self.binaryTreePathsRecu(root, path, result)
return result

def binaryTreePathsRecu(self, node, path, result):
if node is None:
return

if node.left is node.right is None:
ans = ""
for n in path:
ans += str(n.val) + "->"
resault.append(ans + str(node.val))

if node.left:
path.append(node)
self.binaryTreePathsRecu(node.left, path, result)
path.pop()

if node.right:
path.append(node)
self.binaryTreePathsRecu(node.right, path, result)
path.pop()

0 comments on commit 8f03873

Please sign in to comment.