***Find Leaves of Binary Tree

Problem Link: https://www.lintcode.com/problem/650/description

class Solution:
    """
    @param: root: the root of binary tree
    @return: collect and remove all leaves
    """
    def findLeaves(self, root):
        # write your code here
        
        def height(root):
            if(root is None):
                return -1
            l=height(root.left)
            r=height(root.right)
            currheight=1+max(l,r)

            if(currheight==len(result)):
                result.append([])
            
            result[currheight].append(root.val)
            return currheight
            
        result=[]
        height(root)
        return result

Last updated