-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3157-FindTheLevelOfTreeWithMinimumSum.go
More file actions
101 lines (91 loc) · 2.88 KB
/
3157-FindTheLevelOfTreeWithMinimumSum.go
File metadata and controls
101 lines (91 loc) · 2.88 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
// 3157. Find the Level of Tree with Minimum Sum
// Given the root of a binary tree root where each node has a value,
// return the level of the tree that has the minimum sum of values among all the levels (in case of a tie, return the lowest level).
// Note that the root of the tree is at level 1 and the level of any other node is its distance from the root + 1.
// Example 1:
// Input: root = [50,6,2,30,80,7]
// Output: 2
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2024/05/17/image_2024-05-17_16-15-46.png" />
// Example 2:
// Input: root = [36,17,10,null,null,24]
// Output: 3
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2024/05/17/image_2024-05-17_16-14-18.png" />
// Example 3:
// Input: root = [5,null,5,null,5]
// Output: 1
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2024/05/19/image_2024-05-19_19-07-20.png" />
// Constraints:
// The number of nodes in the tree is in the range [1, 10^5].
// 1 <= Node.val <= 10^9
import "fmt"
//import "math"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minimumLevel(root *TreeNode) int {
queue := []*TreeNode{ root }
res, sum := 0, 1 << 31
for level := 1; len(queue) > 0; level++ {
mn := 0
for i := len(queue); i > 0; i-- {
node := queue[0]
queue = queue[1:]
mn += node.Val
if node.Left != nil { queue = append(queue, node.Left) }
if node.Right != nil { queue = append(queue, node.Right) }
}
if sum > mn {
sum, res = mn, level
}
}
return res
}
func main() {
// Example 1:
// Input: root = [50,6,2,30,80,7]
// Output: 2
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2024/05/17/image_2024-05-17_16-15-46.png" />
tree1 := &TreeNode{
50,
&TreeNode{ 6, &TreeNode{ 30, nil, nil, }, &TreeNode{ 80, nil, nil, }, },
&TreeNode{ 2, &TreeNode{ 7, nil, nil, }, nil, },
}
fmt.Println(minimumLevel(tree1)) // 2
// Example 2:
// Input: root = [36,17,10,null,null,24]
// Output: 3
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2024/05/17/image_2024-05-17_16-14-18.png" />
tree2 := &TreeNode{
36,
&TreeNode{ 17, nil, nil, },
&TreeNode{ 10, &TreeNode{ 24, nil, nil, }, nil, },
}
fmt.Println(minimumLevel(tree2)) // 3
// Example 3:
// Input: root = [5,null,5,null,5]
// Output: 1
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2024/05/19/image_2024-05-19_19-07-20.png" />
tree3 := &TreeNode{
5,
nil,
&TreeNode{ 5, nil, &TreeNode{ 5, nil, nil, },},
}
fmt.Println(minimumLevel(tree3)) // 1
}