Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f769732

Browse files
authored
feat: add solutions to lc problem: No.2265 (doocs#3724)
No.2265.Count Nodes Equal to Average of Subtree
1 parent ff4e0d3 commit f769732

File tree

7 files changed

+217
-111
lines changed

7 files changed

+217
-111
lines changed

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

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,36 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### 方法一
68+
### 方法一:DFS
69+
70+
我们设计一个函数 $\textit{dfs}$,它的作用是计算以当前节点为根的子树的和以及节点个数。
71+
72+
函数 $\textit{dfs}$ 的执行过程如下:
73+
74+
- 如果当前节点为空,返回 $(0, 0)$。
75+
- 否则,我们递归计算左右子树的和以及节点个数,分别记为 $(\textit{ls}, \textit{ln})$ 和 $(\textit{rs}, \textit{rn})$。那么,以当前节点为根的子树的和 $\textit{s}$ 和节点个数 $\textit{n}$ 分别为 $\textit{ls} + \textit{rs} + \textit{root.val}$ 和 $\textit{ln} + \textit{rn} + 1$。如果 $\textit{s} / \textit{n} = \textit{root.val}$,则说明当前节点满足题目要求,我们将答案 $\textit{ans}$ 自增 $1$。
76+
- 最后,函数 $\textit{dfs}$ 返回 $\textit{s}$ 和 $\textit{n}$。
77+
78+
我们初始化答案 $\textit{ans}$ 为 $0$,然后调用 $\textit{dfs}$ 函数,最后返回答案 $\textit{ans}$。
79+
80+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 表示二叉树的节点个数。
6981

7082
<!-- tabs:start -->
7183

7284
#### Python3
7385

7486
```python
75-
# Definition for a binary tree node.
76-
# class TreeNode:
77-
# def __init__(self, val=0, left=None, right=None):
78-
# self.val = val
79-
# self.left = left
80-
# self.right = right
8187
class Solution:
82-
def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
83-
def dfs(root):
84-
if root is None:
88+
def averageOfSubtree(self, root: TreeNode) -> int:
89+
def dfs(root) -> tuple:
90+
if not root:
8591
return 0, 0
8692
ls, ln = dfs(root.left)
8793
rs, rn = dfs(root.right)
8894
s = ls + rs + root.val
8995
n = ln + rn + 1
90-
if s // n == root.val:
91-
nonlocal ans
92-
ans += 1
96+
nonlocal ans
97+
ans += int(s // n == root.val)
9398
return s, n
9499

95100
ans = 0
@@ -119,17 +124,16 @@ class Solution {
119124
private int ans;
120125

121126
public int averageOfSubtree(TreeNode root) {
122-
ans = 0;
123127
dfs(root);
124128
return ans;
125129
}
126130

127131
private int[] dfs(TreeNode root) {
128132
if (root == null) {
129-
return new int[] {0, 0};
133+
return new int[2];
130134
}
131-
int[] l = dfs(root.left);
132-
int[] r = dfs(root.right);
135+
var l = dfs(root.left);
136+
var r = dfs(root.right);
133137
int s = l[0] + r[0] + root.val;
134138
int n = l[1] + r[1] + 1;
135139
if (s / n == root.val) {
@@ -156,22 +160,24 @@ class Solution {
156160
*/
157161
class Solution {
158162
public:
159-
int ans;
160163
int averageOfSubtree(TreeNode* root) {
161-
ans = 0;
162-
dfs(root);
164+
int ans = 0;
165+
auto dfs = [&](auto&& dfs, TreeNode* root) -> pair<int, int> {
166+
if (!root) {
167+
return {0, 0};
168+
}
169+
auto [ls, ln] = dfs(dfs, root->left);
170+
auto [rs, rn] = dfs(dfs, root->right);
171+
int s = ls + rs + root->val;
172+
int n = ln + rn + 1;
173+
if (s / n == root->val) {
174+
++ans;
175+
}
176+
return {s, n};
177+
};
178+
dfs(dfs, root);
163179
return ans;
164180
}
165-
166-
vector<int> dfs(TreeNode* root) {
167-
if (!root) return {0, 0};
168-
auto l = dfs(root->left);
169-
auto r = dfs(root->right);
170-
int s = l[0] + r[0] + root->val;
171-
int n = l[1] + r[1] + 1;
172-
if (s / n == root->val) ++ans;
173-
return {s, n};
174-
}
175181
};
176182
```
177183
@@ -186,24 +192,59 @@ public:
186192
* Right *TreeNode
187193
* }
188194
*/
189-
func averageOfSubtree(root *TreeNode) int {
190-
ans := 0
191-
var dfs func(*TreeNode) (int, int)
195+
func averageOfSubtree(root *TreeNode) (ans int) {
196+
var dfs func(root *TreeNode) (int, int)
192197
dfs = func(root *TreeNode) (int, int) {
193198
if root == nil {
194199
return 0, 0
195200
}
196201
ls, ln := dfs(root.Left)
197202
rs, rn := dfs(root.Right)
198-
s := ls + rs + root.Val
199-
n := ln + rn + 1
203+
s, n := ls+rs+root.Val, ln+rn+1
200204
if s/n == root.Val {
201205
ans++
202206
}
203207
return s, n
204208
}
205209
dfs(root)
206-
return ans
210+
return
211+
}
212+
```
213+
214+
#### TypeScript
215+
216+
```ts
217+
/**
218+
* Definition for a binary tree node.
219+
* class TreeNode {
220+
* val: number
221+
* left: TreeNode | null
222+
* right: TreeNode | null
223+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
224+
* this.val = (val===undefined ? 0 : val)
225+
* this.left = (left===undefined ? null : left)
226+
* this.right = (right===undefined ? null : right)
227+
* }
228+
* }
229+
*/
230+
231+
function averageOfSubtree(root: TreeNode | null): number {
232+
let ans: number = 0;
233+
const dfs = (root: TreeNode | null): [number, number] => {
234+
if (!root) {
235+
return [0, 0];
236+
}
237+
const [ls, ln] = dfs(root.left);
238+
const [rs, rn] = dfs(root.right);
239+
const s = ls + rs + root.val;
240+
const n = ln + rn + 1;
241+
if (Math.floor(s / n) === root.val) {
242+
++ans;
243+
}
244+
return [s, n];
245+
};
246+
dfs(root);
247+
return ans;
207248
}
208249
```
209250

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

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tags:
3535
<pre>
3636
<strong>Input:</strong> root = [4,8,5,0,1,null,6]
3737
<strong>Output:</strong> 5
38-
<strong>Explanation:</strong>
38+
<strong>Explanation:</strong>
3939
For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
4040
For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
4141
For the node with value 0: The average of its subtree is 0 / 1 = 0.
@@ -65,31 +65,36 @@ For the node with value 6: The average of its subtree is 6 / 1 = 6.
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: DFS
69+
70+
We design a function $\textit{dfs}$, which calculates the sum and the number of nodes of the subtree rooted at the current node.
71+
72+
The execution process of the function $\textit{dfs}$ is as follows:
73+
74+
- If the current node is null, return $(0, 0)$.
75+
- Otherwise, we recursively calculate the sum and the number of nodes of the left and right subtrees, denoted as $(\textit{ls}, \textit{ln})$ and $(\textit{rs}, \textit{rn})$, respectively. Then, the sum $\textit{s}$ and the number of nodes $\textit{n}$ of the subtree rooted at the current node are $\textit{ls} + \textit{rs} + \textit{root.val}$ and $\textit{ln} + \textit{rn} + 1$, respectively. If $\textit{s} / \textit{n} = \textit{root.val}$, it means the current node meets the requirement of the problem, and we increment the answer $\textit{ans}$ by $1$.
76+
- Finally, the function $\textit{dfs}$ returns $\textit{s}$ and $\textit{n}$.
77+
78+
We initialize the answer $\textit{ans}$ to $0$, then call the $\textit{dfs}$ function, and finally return the answer $\textit{ans}$.
79+
80+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ represents the number of nodes in the binary tree.
6981

7082
<!-- tabs:start -->
7183

7284
#### Python3
7385

7486
```python
75-
# Definition for a binary tree node.
76-
# class TreeNode:
77-
# def __init__(self, val=0, left=None, right=None):
78-
# self.val = val
79-
# self.left = left
80-
# self.right = right
8187
class Solution:
82-
def averageOfSubtree(self, root: Optional[TreeNode]) -> int:
83-
def dfs(root):
84-
if root is None:
88+
def averageOfSubtree(self, root: TreeNode) -> int:
89+
def dfs(root) -> tuple:
90+
if not root:
8591
return 0, 0
8692
ls, ln = dfs(root.left)
8793
rs, rn = dfs(root.right)
8894
s = ls + rs + root.val
8995
n = ln + rn + 1
90-
if s // n == root.val:
91-
nonlocal ans
92-
ans += 1
96+
nonlocal ans
97+
ans += int(s // n == root.val)
9398
return s, n
9499

95100
ans = 0
@@ -119,17 +124,16 @@ class Solution {
119124
private int ans;
120125

121126
public int averageOfSubtree(TreeNode root) {
122-
ans = 0;
123127
dfs(root);
124128
return ans;
125129
}
126130

127131
private int[] dfs(TreeNode root) {
128132
if (root == null) {
129-
return new int[] {0, 0};
133+
return new int[2];
130134
}
131-
int[] l = dfs(root.left);
132-
int[] r = dfs(root.right);
135+
var l = dfs(root.left);
136+
var r = dfs(root.right);
133137
int s = l[0] + r[0] + root.val;
134138
int n = l[1] + r[1] + 1;
135139
if (s / n == root.val) {
@@ -156,22 +160,24 @@ class Solution {
156160
*/
157161
class Solution {
158162
public:
159-
int ans;
160163
int averageOfSubtree(TreeNode* root) {
161-
ans = 0;
162-
dfs(root);
164+
int ans = 0;
165+
auto dfs = [&](auto&& dfs, TreeNode* root) -> pair<int, int> {
166+
if (!root) {
167+
return {0, 0};
168+
}
169+
auto [ls, ln] = dfs(dfs, root->left);
170+
auto [rs, rn] = dfs(dfs, root->right);
171+
int s = ls + rs + root->val;
172+
int n = ln + rn + 1;
173+
if (s / n == root->val) {
174+
++ans;
175+
}
176+
return {s, n};
177+
};
178+
dfs(dfs, root);
163179
return ans;
164180
}
165-
166-
vector<int> dfs(TreeNode* root) {
167-
if (!root) return {0, 0};
168-
auto l = dfs(root->left);
169-
auto r = dfs(root->right);
170-
int s = l[0] + r[0] + root->val;
171-
int n = l[1] + r[1] + 1;
172-
if (s / n == root->val) ++ans;
173-
return {s, n};
174-
}
175181
};
176182
```
177183
@@ -186,24 +192,59 @@ public:
186192
* Right *TreeNode
187193
* }
188194
*/
189-
func averageOfSubtree(root *TreeNode) int {
190-
ans := 0
191-
var dfs func(*TreeNode) (int, int)
195+
func averageOfSubtree(root *TreeNode) (ans int) {
196+
var dfs func(root *TreeNode) (int, int)
192197
dfs = func(root *TreeNode) (int, int) {
193198
if root == nil {
194199
return 0, 0
195200
}
196201
ls, ln := dfs(root.Left)
197202
rs, rn := dfs(root.Right)
198-
s := ls + rs + root.Val
199-
n := ln + rn + 1
203+
s, n := ls+rs+root.Val, ln+rn+1
200204
if s/n == root.Val {
201205
ans++
202206
}
203207
return s, n
204208
}
205209
dfs(root)
206-
return ans
210+
return
211+
}
212+
```
213+
214+
#### TypeScript
215+
216+
```ts
217+
/**
218+
* Definition for a binary tree node.
219+
* class TreeNode {
220+
* val: number
221+
* left: TreeNode | null
222+
* right: TreeNode | null
223+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
224+
* this.val = (val===undefined ? 0 : val)
225+
* this.left = (left===undefined ? null : left)
226+
* this.right = (right===undefined ? null : right)
227+
* }
228+
* }
229+
*/
230+
231+
function averageOfSubtree(root: TreeNode | null): number {
232+
let ans: number = 0;
233+
const dfs = (root: TreeNode | null): [number, number] => {
234+
if (!root) {
235+
return [0, 0];
236+
}
237+
const [ls, ln] = dfs(root.left);
238+
const [rs, rn] = dfs(root.right);
239+
const s = ls + rs + root.val;
240+
const n = ln + rn + 1;
241+
if (Math.floor(s / n) === root.val) {
242+
++ans;
243+
}
244+
return [s, n];
245+
};
246+
dfs(root);
247+
return ans;
207248
}
208249
```
209250

solution/2200-2299/2265.Count Nodes Equal to Average of Subtree/Solution.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111
*/
1212
class Solution {
1313
public:
14-
int ans;
1514
int averageOfSubtree(TreeNode* root) {
16-
ans = 0;
17-
dfs(root);
15+
int ans = 0;
16+
auto dfs = [&](auto&& dfs, TreeNode* root) -> pair<int, int> {
17+
if (!root) {
18+
return {0, 0};
19+
}
20+
auto [ls, ln] = dfs(dfs, root->left);
21+
auto [rs, rn] = dfs(dfs, root->right);
22+
int s = ls + rs + root->val;
23+
int n = ln + rn + 1;
24+
if (s / n == root->val) {
25+
++ans;
26+
}
27+
return {s, n};
28+
};
29+
dfs(dfs, root);
1830
return ans;
1931
}
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-
};
32+
};

0 commit comments

Comments
 (0)