Python solution using DFS


  • 0
    C
    class Solution(object):
        def findLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            res = []
            while(root is not None):
                subList = []
                root = self.helper(root, subList)
                res.append(subList)
            return res
                
        
        def helper(self, root, res):
            if root is None:
                return 
            if root.left is None and root.right is None:
                res.append(root.val)
                return None
            root.left = self.helper(root.left, res)
            root.right = self.helper(root.right, res)
            return root
    

Log in to reply
 

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