首页 > 其他 > 详细

515. Find Largest Value in Each Tree Row

时间:2020-03-10 21:06:01      阅读:55      评论:0      收藏:0      [点我收藏+]

You need to find the largest value in each row of a binary tree.

Example:

Input: 

          1
         /         3   2
       / \   \  
      5   3   9 

Output: [1, 3, 9]
# 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 root is None:
            return []
        queue = [root]
        l = []
        while queue:
            size = len(queue)
            maxnum = float(-inf)
            for i in range(size):
                top = queue.pop(0)
                maxnum = max(maxnum, top.val)
                if top.left:
                    queue.append(top.left)
                if top.right:
                    queue.append(top.right)
            l.append(maxnum)
        return l
                
            

 

515. Find Largest Value in Each Tree Row

原文:https://www.cnblogs.com/boluo007/p/12458315.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!