当前位置:网站首页>leetcode:515. Find the maximum value in each tree row [brainless BFS]

leetcode:515. Find the maximum value in each tree row [brainless BFS]

2022-06-24 22:04:00 Review of the white speed Dragon King

 Insert picture description here

analysis

No brain bfs

ac code

# 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: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        ans = []
        q = [root]
        while q:
            new_q = []
            maxn = -inf
            for node in q:
                maxn = max(maxn, node.val)
                if node.left: new_q.append(node.left)
                if node.right: new_q.append(node.right)
            ans.append(maxn)
            q = new_q
        
        return ans

summary

No brain bfs

原网站

版权声明
本文为[Review of the white speed Dragon King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241535310130.html