zoukankan      html  css  js  c++  java
  • LeetCode 515. 在每个树行中找最大值

    515. 在每个树行中找最大值

    Difficulty: 中等

    您需要在二叉树的每一行中找到最大的值。

    示例:

    输入: 
    
              1
             / 
            3   2
           /      
          5   3   9 
    
    输出: [1, 3, 9]
    

    Solution

    Language: ****

    BFS+queue实现层序遍历,十分easy,一次AC。

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, val=0, left=None, right=None):
    #         self.val = val
    #         self.left = left
    #         self.right = right
    class Solution:
        def largestValues(self, root: TreeNode) -> List[int]:
            if not root: return None
            queue, res = [root], []
            while queue:
                curLevel, size = [], len(queue)
                for i in range(size):
                    node = queue.pop(0)
                    if node.left:
                        queue.append(node.left)
                    if node.right:
                        queue.append(node.right)
                    curLevel.append(node.val)
                res.append(max(curLevel))
            return res
    
  • 相关阅读:
    tcp/心跳包
    TCP协议中的三次握手和四次挥手(图解)
    http 中get和post
    xmpp总结
    IOS中http请求使用cookie
    sdwebimage总结
    iOS断言
    Object-C自定义对象NSLog输入信息
    NSTimer你真的会用了吗
    ios中block中的探究
  • 原文地址:https://www.cnblogs.com/swordspoet/p/14096725.html
Copyright © 2011-2022 走看看