Python Code


  • 1
    R
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
     
    class Solution(object):
        def findLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            ret = []
            self.helper(root, ret)
            return ret
            
            
        def helper(self, root, ret):
            if not root: return 0
               
            left  = self.helper(root.left, ret)
            right = self.helper(root.right, ret)
            level = max(left, right) + 1
            
            if level > len(ret):
                ret.append([])
            ret[level-1].append(root.val)
            
            return level
    

Log in to reply
 

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