diff options
Diffstat (limited to 'binary_tree_paths.py')
-rw-r--r-- | binary_tree_paths.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/binary_tree_paths.py b/binary_tree_paths.py new file mode 100644 index 0000000..9b846b0 --- /dev/null +++ b/binary_tree_paths.py @@ -0,0 +1,39 @@ +# 257. Binary Tree Paths + +""" +given the 'root' of a binary tree, return all root to leaf paths in any +order. a leaf is a node with no children +""" + + +# Definition for a binary tree node. +class TreeNode(object): + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution(object): + def binaryTreePaths(self, root): + """ + :type root: TreeNode + :rtype: List[str] + """ + if not root: + return [] + ans, stack = [], [(root, "")] + while stack: + node, ls = stack.pop() + if not node.left and not node.right: + ans.append(ls + str(node.val)) + if node.right: + stack.append((node.right, ls + str(node.val) + "->")) + if node.left: + stack.append((node.left, ls + str(node.val) + "->")) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.binaryTreePaths(root=[1, 2, 3, None, 5])) |