美文网首页
515. Find Largest Value in Each

515. Find Largest Value in Each

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-08-02 19:24 被阅读0次

515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row
# 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 largestValues(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        qu = [root]
        res = []
        while qu:
            mx = float("-inf")
            for i in range(len(qu)):
                node = qu.pop(0)
                mx = max(mx, node.val)
                if node.left:
                    qu.append(node.left)
                if node.right:
                    qu.append(node.right)
            res.append(mx)
        return res
            

相关文章

网友评论

      本文标题:515. Find Largest Value in Each

      本文链接:https://www.haomeiwen.com/subject/vtnldctx.html