Python Iterative Solution


  • 0
    T
    def binaryTreePaths(self, root):
        if root is None:
            return []
        answer, stack = [], [[root]]
        while stack:
            path = stack.pop()
            node = path.pop()
            path.append(str(node.val))
            if node.left is node.right is None:
                answer.append('->'.join(path))
            node.left and stack.append(path + [node.left])
            node.right and stack.append(path + [node.right])
        return answer

Log in to reply
 

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