Binary Tree Paths

DFS向下顺序递归就行了,还有迭代方法可以继续研究一下

class Solution(object):
    def binaryTreePaths(self, root):
        """
        :type root: TreeNode
        :rtype: List[str]
        """
        result = []
        self.helper(root, '', result)
        return result

    def helper(self, root, tmp, result):
        if not root:
            return 

        if not root.left and not root.right:
            result.append(tmp + `root.val`)
            return 

        if root.left:
            self.helper(root.left, tmp + `root.val` + '->', result)
        if root.right:
            self.helper(root.right, tmp + `root.val` + '->', result)

results matching ""

    No results matching ""