forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1161.go
More file actions
28 lines (26 loc) · 638 Bytes
/
Copy path1161.go
File metadata and controls
28 lines (26 loc) · 638 Bytes
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
func maxLevelSum(root *TreeNode) int {
if root == nil {
return 0
}
q := []*TreeNode{root}
res, maxSum, level := 0, 0, 0
for len(q) > 0 {
curSum, qLen := 0, len(q)
for i := 0; i < qLen; i++ {
node := q[0]
q = q[1:]
curSum += node.Val
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
}
level++
if curSum > maxSum {
maxSum, res = curSum, level
}
}
return res
}