515-find-largest-value-in-each-tree-row
515-find-largest-value-in-each-tree-row
- Find Largest Value in Each Tree Row Solved Medium Topics Companies Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
Example 1:
Input: root = [1,3,2,5,3,null,9] Output: [1,3,9] Example 2:
Input: root = [1,2,3] Output: [1,3] 10,null,null,11,null,12,null,13,null,null,14] Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
My solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import deque
class Solution(object):
def largestValues(self, root):
"""
:type root: Optional[TreeNode]
:rtype: List[int]
"""
if not root:
return []
res = []
queue = deque()
queue.append(root)
while queue:
max_val = float('-inf')
for i in range(len(queue)):
cur = queue.popleft()
max_val = max(cur.val,max_val)
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
res.append(max_val)
return res
本文由作者按照 CC BY 4.0 进行授权