Python solution using DFS


  • 0
    J
    class Solution(object):
        mydict = {}
        def largestValues(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            self.mydict.clear()
            self.find(root, 0)
            i = 0
            result = []
            while i in self.mydict:
                result.append(self.mydict[i])
                i += 1
            return result
        
        def find(self, root, layer):
            if root is None or root.val == None:
                return
            else:
                if layer in self.mydict:
                    self.mydict[layer] = max(self.mydict[layer], root.val)
                else:
                    self.mydict[layer] = root.val
                layer += 1
                self.find(root.left, layer)
                self.find(root.right, layer)
                return
    

Log in to reply
 

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