Python Peeling Onions


  • 0
    W

    This method peels layer by layer, kind of silly

    def findLeaves(self, root):
        ret = []
        while root:
            L, root = self.peel(root)
            ret.append(L)
        return ret        
    
    def peel(self, root):
        if not root: return [], None
        if not root.left and not root.right:
            temp = root.val
            return [temp], None
        l, r = self.peel(root.left), self.peel(root.right)
        root.left = l[1]
        root.right = r[1]
        return l[0] + r[0], root

Log in to reply
 

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