Intuitive DFS - count and tell


  • 0
    D
    class Solution(object):
        def findBottomLeftValue(self, root):
            if not root.left and not root.right:
                return root.val
            maxleft = [0,0]
            count = 0
            self.helper(root,count,maxleft)
            return maxleft[1]
            
        
        def helper(self,root,count,maxleft):
            if not root:
                return
            tmp = root
            tmp2 = count
            while tmp.left:
                tmp=tmp.left
                count+=1
            if count+1 > maxleft[0]:
                maxleft[0]=count+1
                maxleft[1]=tmp.val
            self.helper(root.left,tmp2+1,maxleft)
            self.helper(root.right,tmp2+1,maxleft)
    

Log in to reply
 

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