Skip to content

Commit ddbacc6

Browse files
committed
added problem 2265
1 parent 65c3d80 commit ddbacc6

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

problems/problem2265/get_avg_nodes.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problem2265
2+
3+
import . "leetcodedaily/helpers/binarytree"
4+
5+
/*
6+
Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.
7+
Note:
8+
The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
9+
A subtree of root is a tree consisting of root and all of its descendants.
10+
*/
11+
12+
func averageOfSubtree(root *TreeNode) int {
13+
var res int
14+
var dfs func(*TreeNode) (int, int)
15+
16+
dfs = func(cur *TreeNode) (int, int) {
17+
if cur == nil {
18+
return 0, 0
19+
}
20+
lS, lC := dfs(cur.Left)
21+
rS, rC := dfs(cur.Right)
22+
if cur.Val == (lS+rS+cur.Val)/(lC+rC+1) {
23+
res++
24+
}
25+
return (lS + rS + cur.Val), (lC + rC + 1)
26+
}
27+
28+
dfs(root)
29+
30+
return res
31+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package problem2265
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
. "leetcodedaily/helpers/binarytree"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
type TestCase struct {
13+
Input *TreeNode
14+
Expected int
15+
}
16+
17+
var TestCases = []TestCase{
18+
{MakeTree(4, 8, 0, NULL, NULL, 1, NULL, NULL, 5, NULL, 6, NULL, NULL), 5},
19+
{MakeTree(1, NULL, NULL), 1},
20+
}
21+
22+
func TestTreeNodeAvg(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, tc := range TestCases {
26+
want := tc.Expected
27+
got := averageOfSubtree(tc.Input)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
29+
}
30+
}
31+
32+
func BenchmarkTreeNodeAvg(b *testing.B) {
33+
for i := 0; i < b.N; i++ {
34+
for _, tc := range TestCases {
35+
averageOfSubtree(tc.Input)
36+
}
37+
}
38+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ Each problem is in it's own directory, with test files. There are helper package
555555
| 2246 | [Longest Path With Different Adjacent Characters](https://leetcode.com/problems/longest-path-with-different-adjacent-characters) | [My Solution](./problems/problem0000) ||
556556
| 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [My Solution](./problems/problem2256) ||
557557
| 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result) | [My Solution](./problems/problem2259) ||
558+
| 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree) | [My Solution](./problems/problem2265) ||
558559
| 2272 | [Substring With Largest Variance](https://leetcode.com/problems/substring-with-largest-variance) | [My Solution](./problems/problem2272) ||
559560
| 2278 | [Percentage of Letter in String](https://leetcode.com/problems/percentage-of-letter-in-string) | [My Solution](./problems/problem2278) ||
560561
| 2279 | [Maximum Bags With Full Capacity of Rocks](https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks) | [My Solution](./problems/problem2279) ||

0 commit comments

Comments
 (0)