my python solution


  • 1
    A
    class Solution(object):
        def findLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            visited={None}
            ans=[]
            def helper(node):
                if node.left in visited and node.right in visited:
                    visited.add(node)
                    ans[-1].append(node.val)
                    return
                if node.left not in visited:
                    helper(node.left)
                if node.right not in visited:
                    helper(node.right)
                return
            while root not in visited:
                ans.append([])
                helper(root)
            return ans
    

    if the left node and right node are visited or None, that node should be add the current level


Log in to reply
 

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