gary@interview:~/interview/coding/515-find-largest….md$
$ cat ./coding/515-find-largest-value-in-each-tree-row.md
[Coding]

515. Find Largest Value in Each Tree Row

────────────────────────────────────────────────────────────

515. Find Largest Value in Each Tree Row

題目要熟悉 BFS ,可以先熟悉 199. Binary Tree Right Side View

# 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 = deque([root])

        while q:
            currMax = float('-inf')
            for _ in range(len(q)):
                node = q.popleft()
                currMax = max(currMax, node.val)
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
            ans.append(currMax)
        
        return ans
--tags#Tree
$ ls ./coding/ | grep -v 515-find-largest-value-in-each-tree-row
265. Paint House II256. Paint House143. Reorder List1762. Buildings With an Ocean View
← cd ../codingcd ~