Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita54b1a5

Browse files
committed
add new files
1 parent17ed604 commita54b1a5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
3+
4+
Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
5+
6+
Example 1:
7+
8+
Input: [1,7,0,7,-8,null,null]
9+
Output: 2
10+
Explanation:
11+
Level 1 sum = 1.
12+
Level 2 sum = 7 + 0 = 7.
13+
Level 3 sum = 7 + -8 = -1.
14+
So we return the level with the maximum sum which is level 2.
15+
16+
*/
17+
package main
18+
19+
typeTreeNodestruct {
20+
Valint
21+
Left*TreeNode
22+
Right*TreeNode
23+
}
24+
25+
varm=make(map[int]int)
26+
27+
funcmaxLevelSum(root*TreeNode)int {
28+
ifroot==nil {
29+
return0
30+
}
31+
dfs(root,1)
32+
level:=0
33+
forl:=rangem {
34+
ifm[l]>m[level] {
35+
level=1
36+
}
37+
}
38+
returnlevel
39+
}
40+
41+
funcdfs(root*TreeNode,levelint) {
42+
ifroot==nil {
43+
return
44+
}
45+
dfs(root.Left,level+1)
46+
m[level]+=root.Val
47+
dfs(root.Right,level+1)
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp