Silly 3-liner...

  • 6

    Do it for the two subtrees, combine their results, and add the root. Should be slow but got accepted in 52ms (fairly fast for Python).

    def findLeaves(self, root):
        if not root: return []
        kids = map(self.findLeaves, (root.left, root.right))
        return map(lambda l, r: (l or []) + (r or []), *kids) + [[root.val]]

Log in to reply

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