Skip to content

Commit e24d075

Browse files
committed
Time: 44 ms (80.73%), Space: 16.5 MB (59.80%) - LeetHub
1 parent 97fab9f commit e24d075

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
from collections import deque, defaultdict
8+
9+
class Solution(object):
10+
11+
def largestValues(self, root):
12+
13+
if(root == None): return []
14+
15+
maxVal = defaultdict(lambda:-float("inf"))
16+
17+
q = deque([[root, 0]])
18+
19+
while(len(q) > 0):
20+
current = q.popleft()
21+
currentNode = current[0]
22+
currentDepth = current[1]
23+
24+
maxVal[currentDepth] = max(maxVal[currentDepth], currentNode.val)
25+
26+
if(currentNode.left != None):
27+
q.append([currentNode.left, currentDepth+1])
28+
29+
if(currentNode.right != None):
30+
q.append([currentNode.right, currentDepth+1])
31+
32+
33+
maxDepth = max(maxVal.keys())
34+
# print(maxDepth)
35+
res = [None]*(maxDepth+1)
36+
37+
for i in maxVal:
38+
# print(i)
39+
res[i] = maxVal[i]
40+
41+
return res

0 commit comments

Comments
 (0)