Tree Path

Root-To-Leaf paths.

257. Binary Tree Paths

class Solution(object):
    def binaryTreePaths(self, root):
        self.res = []
        self.dfs(root, [])
        paths = []
        for path in self.res:
            paths.append('->'.join(path))
        return paths

    def dfs(self, root, path):
        if not root:
            return 
        if not root.left and not root.right:
            self.res.append(path + [str(root.val)])
            return
        self.dfs(root.left, path + [str(root.val)])
        self.dfs(root.right, path + [str(root.val)])

results matching ""

    No results matching ""