Skip to content

Commit ae348af

Browse files
committed
feat: add solutions to lc problem: No.2265
No.2265.Count Nodes Equal to Average of Subtree
1 parent 9f2d908 commit ae348af

File tree

6 files changed

+372
-2
lines changed

6 files changed

+372
-2
lines changed

solution/2200-2299/2265.Count Nodes Equal to Average of Subtree/README.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,141 @@
5656
<!-- 这里可写当前语言的特殊实现逻辑 -->
5757

5858
```python
59-
59+
# Definition for a binary tree node.
60+
# class TreeNode:
61+
# def __init__(self, val=0, left=None, right=None):
62+
# self.val = val
63+
# self.left = left
64+
# self.right = right
65+
class Solution:
66+
def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
67+
def dfs(root):
68+
if root is None:
69+
return 0, 0
70+
ls, ln = dfs(root.left)
71+
rs, rn = dfs(root.right)
72+
s = ls + rs + root.val
73+
n = ln + rn + 1
74+
if s // n == root.val:
75+
nonlocal ans
76+
ans += 1
77+
return s, n
78+
79+
ans = 0
80+
dfs(root)
81+
return ans
6082
```
6183

6284
### **Java**
6385

6486
<!-- 这里可写当前语言的特殊实现逻辑 -->
6587

6688
```java
89+
/**
90+
* Definition for a binary tree node.
91+
* public class TreeNode {
92+
* int val;
93+
* TreeNode left;
94+
* TreeNode right;
95+
* TreeNode() {}
96+
* TreeNode(int val) { this.val = val; }
97+
* TreeNode(int val, TreeNode left, TreeNode right) {
98+
* this.val = val;
99+
* this.left = left;
100+
* this.right = right;
101+
* }
102+
* }
103+
*/
104+
class Solution {
105+
private int ans;
106+
107+
public int averageOfSubtree(TreeNode root) {
108+
ans = 0;
109+
dfs(root);
110+
return ans;
111+
}
112+
113+
private int[] dfs(TreeNode root) {
114+
if (root == null) {
115+
return new int[]{0, 0};
116+
}
117+
int[] l = dfs(root.left);
118+
int[] r = dfs(root.right);
119+
int s = l[0] + r[0] + root.val;
120+
int n = l[1] + r[1] + 1;
121+
if (s / n == root.val) {
122+
++ans;
123+
}
124+
return new int[]{s, n};
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
/**
133+
* Definition for a binary tree node.
134+
* struct TreeNode {
135+
* int val;
136+
* TreeNode *left;
137+
* TreeNode *right;
138+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
140+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
int ans;
146+
int averageOfSubtree(TreeNode* root) {
147+
ans = 0;
148+
dfs(root);
149+
return ans;
150+
}
151+
152+
vector<int> dfs(TreeNode* root) {
153+
if (!root) return {0, 0};
154+
auto l = dfs(root->left);
155+
auto r = dfs(root->right);
156+
int s = l[0] + r[0] + root->val;
157+
int n = l[1] + r[1] + 1;
158+
if (s / n == root->val) ++ans;
159+
return {s, n};
160+
}
161+
};
162+
```
67163

164+
### **Go**
165+
166+
```go
167+
/**
168+
* Definition for a binary tree node.
169+
* type TreeNode struct {
170+
* Val int
171+
* Left *TreeNode
172+
* Right *TreeNode
173+
* }
174+
*/
175+
func averageOfSubtree(root *TreeNode) int {
176+
ans := 0
177+
var dfs func(*TreeNode) (int, int)
178+
dfs = func(root *TreeNode) (int, int) {
179+
if root == nil {
180+
return 0, 0
181+
}
182+
ls, ln := dfs(root.Left)
183+
rs, rn := dfs(root.Right)
184+
s := ls + rs + root.Val
185+
n := ln + rn + 1
186+
if s/n == root.Val {
187+
ans++
188+
}
189+
return s, n
190+
}
191+
dfs(root)
192+
return ans
193+
}
68194
```
69195

70196
### **TypeScript**

solution/2200-2299/2265.Count Nodes Equal to Average of Subtree/README_EN.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,139 @@ For the node with value 6: The average of its subtree is 6 / 1 = 6.
5050
### **Python3**
5151

5252
```python
53-
53+
# Definition for a binary tree node.
54+
# class TreeNode:
55+
# def __init__(self, val=0, left=None, right=None):
56+
# self.val = val
57+
# self.left = left
58+
# self.right = right
59+
class Solution:
60+
def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
61+
def dfs(root):
62+
if root is None:
63+
return 0, 0
64+
ls, ln = dfs(root.left)
65+
rs, rn = dfs(root.right)
66+
s = ls + rs + root.val
67+
n = ln + rn + 1
68+
if s // n == root.val:
69+
nonlocal ans
70+
ans += 1
71+
return s, n
72+
73+
ans = 0
74+
dfs(root)
75+
return ans
5476
```
5577

5678
### **Java**
5779

5880
```java
81+
/**
82+
* Definition for a binary tree node.
83+
* public class TreeNode {
84+
* int val;
85+
* TreeNode left;
86+
* TreeNode right;
87+
* TreeNode() {}
88+
* TreeNode(int val) { this.val = val; }
89+
* TreeNode(int val, TreeNode left, TreeNode right) {
90+
* this.val = val;
91+
* this.left = left;
92+
* this.right = right;
93+
* }
94+
* }
95+
*/
96+
class Solution {
97+
private int ans;
98+
99+
public int averageOfSubtree(TreeNode root) {
100+
ans = 0;
101+
dfs(root);
102+
return ans;
103+
}
104+
105+
private int[] dfs(TreeNode root) {
106+
if (root == null) {
107+
return new int[]{0, 0};
108+
}
109+
int[] l = dfs(root.left);
110+
int[] r = dfs(root.right);
111+
int s = l[0] + r[0] + root.val;
112+
int n = l[1] + r[1] + 1;
113+
if (s / n == root.val) {
114+
++ans;
115+
}
116+
return new int[]{s, n};
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for a binary tree node.
126+
* struct TreeNode {
127+
* int val;
128+
* TreeNode *left;
129+
* TreeNode *right;
130+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
131+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
132+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
133+
* };
134+
*/
135+
class Solution {
136+
public:
137+
int ans;
138+
int averageOfSubtree(TreeNode* root) {
139+
ans = 0;
140+
dfs(root);
141+
return ans;
142+
}
143+
144+
vector<int> dfs(TreeNode* root) {
145+
if (!root) return {0, 0};
146+
auto l = dfs(root->left);
147+
auto r = dfs(root->right);
148+
int s = l[0] + r[0] + root->val;
149+
int n = l[1] + r[1] + 1;
150+
if (s / n == root->val) ++ans;
151+
return {s, n};
152+
}
153+
};
154+
```
59155

156+
### **Go**
157+
158+
```go
159+
/**
160+
* Definition for a binary tree node.
161+
* type TreeNode struct {
162+
* Val int
163+
* Left *TreeNode
164+
* Right *TreeNode
165+
* }
166+
*/
167+
func averageOfSubtree(root *TreeNode) int {
168+
ans := 0
169+
var dfs func(*TreeNode) (int, int)
170+
dfs = func(root *TreeNode) (int, int) {
171+
if root == nil {
172+
return 0, 0
173+
}
174+
ls, ln := dfs(root.Left)
175+
rs, rn := dfs(root.Right)
176+
s := ls + rs + root.Val
177+
n := ln + rn + 1
178+
if s/n == root.Val {
179+
ans++
180+
}
181+
return s, n
182+
}
183+
dfs(root)
184+
return ans
185+
}
60186
```
61187

62188
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int ans;
15+
int averageOfSubtree(TreeNode* root) {
16+
ans = 0;
17+
dfs(root);
18+
return ans;
19+
}
20+
21+
vector<int> dfs(TreeNode* root) {
22+
if (!root) return {0, 0};
23+
auto l = dfs(root->left);
24+
auto r = dfs(root->right);
25+
int s = l[0] + r[0] + root->val;
26+
int n = l[1] + r[1] + 1;
27+
if (s / n == root->val) ++ans;
28+
return {s, n};
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func averageOfSubtree(root *TreeNode) int {
10+
ans := 0
11+
var dfs func(*TreeNode) (int, int)
12+
dfs = func(root *TreeNode) (int, int) {
13+
if root == nil {
14+
return 0, 0
15+
}
16+
ls, ln := dfs(root.Left)
17+
rs, rn := dfs(root.Right)
18+
s := ls + rs + root.Val
19+
n := ln + rn + 1
20+
if s/n == root.Val {
21+
ans++
22+
}
23+
return s, n
24+
}
25+
dfs(root)
26+
return ans
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
private int ans;
18+
19+
public int averageOfSubtree(TreeNode root) {
20+
ans = 0;
21+
dfs(root);
22+
return ans;
23+
}
24+
25+
private int[] dfs(TreeNode root) {
26+
if (root == null) {
27+
return new int[]{0, 0};
28+
}
29+
int[] l = dfs(root.left);
30+
int[] r = dfs(root.right);
31+
int s = l[0] + r[0] + root.val;
32+
int n = l[1] + r[1] + 1;
33+
if (s / n == root.val) {
34+
++ans;
35+
}
36+
return new int[]{s, n};
37+
}
38+
}

0 commit comments

Comments
 (0)