-
Notifications
You must be signed in to change notification settings - Fork 7
/
solution.py
44 lines (40 loc) · 1.3 KB
/
solution.py
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
38
39
40
41
42
43
44
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
from collections import deque
class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
max_list = [-float('inf')] * 10000
queue = deque([(root, 0)])
while queue:
node, level = queue.popleft()
if node:
max_list[level] = max(max_list[level], node.val)
queue.extend([(node.left, level+1), (node.right, level+1)])
return max_list[:level]
from collections import deque
class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result = []
cur_max = -float('inf')
queue = deque([(root, 0)])
while queue:
node, level = queue.popleft()
if node:
if len(result) < level:
result.append(cur_max)
cur_max = -float('inf')
cur_max = max(cur_max, node.val)
queue.extend([(node.left, level+1), (node.right, level+1)])
return result + ([cur_max] if cur_max != -float('inf') else [])