257. Binary Tree Paths
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if root == None:
return []
res = []
trace = []
self.DFS(root, res, trace)
return res
def DFS(self, root, res, trace):
trace.append(str(root.val))
if root.left == None and root.right == None:
res.append('->'.join(trace))
trace.pop()
return
if root.left:
self.DFS(root.left, res, trace)
if root.right:
self.DFS(root.right, res, trace)
trace.pop()