Python DFS

  • 0
    class Solution:
        def binaryTreePaths(self, root):
            ans = []
            def dfs(r, tmp):
                if r.left:
                    dfs(r.left, tmp + [str(r.left.val)])
                if r.right:
                    dfs(r.right, tmp + [str(r.right.val)])
                if not r.left and not r.right:
            if not root:
                return []
            dfs(root, [str(root.val)])
            return ans

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.