Simple brute force but accepted


  • 0
    S
    class Solution(object):
        def findLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            leaves = []
            def popLeaf(root,collect):
                if root:
                    if root.left is None and root.right is None:
                        collect.append(root.val)
                        return None
                    
                    root.left  = popLeaf(root.left, collect)
                    root.right = popLeaf(root.right, collect)
                return root
            
            while root:
                collect = []
                root = popLeaf(root,collect)
                leaves.append(collect[:])
            return leaves

Log in to reply
 

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