27
27
28
28
<!-- 这里可写通用的实现逻辑 -->
29
29
30
+ ** 方法一:递归**
31
+
32
+ 我们设计一个递归函数 $dfs(root)$,该函数返回以 $root$ 为根的子树中所有节点的值是否相同。
33
+
34
+ 函数 $dfs(root)$ 的递归过程如下:
35
+
36
+ - 如果 $root$ 为空,则返回 ` true ` ;
37
+ - 否则,我们递归地计算 $root$ 的左右子树,记为 $l$ 和 $r$;如果 $l$ 为 ` false ` 或者 $r$ 为 ` false ` ,则返回 ` false ` ;如果 $root$ 的左子树不为空且 $root$ 的左子树的值不等于 $root$ 的值,或者 $root$ 的右子树不为空且 $root$ 的右子树的值不等于 $root$ 的值,则返回 ` false ` ;否则,我们将答案加一,并返回 ` true ` 。
38
+
39
+ 递归结束后,返回答案即可。
40
+
41
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
42
+
30
43
<!-- tabs:start -->
31
44
32
45
### ** Python3**
@@ -45,16 +58,16 @@ class Solution:
45
58
def dfs (root ):
46
59
if root is None :
47
60
return True
48
- left, right = dfs(root.left), dfs(root.right)
49
- t = True
50
- if root.left and root.left.val != root.val:
51
- t = False
52
- if root.right and root.right.val != root.val:
53
- t = False
54
- nonlocal ans
55
- if left and t and right:
61
+ l, r = dfs(root.left), dfs(root.right)
62
+ if not l or not r:
63
+ return False
64
+ a = root.val if root.left is None else root.left.val
65
+ b = root.val if root.right is None else root.right.val
66
+ if a == b == root.val:
67
+ nonlocal ans
56
68
ans += 1
57
- return left and t and right
69
+ return True
70
+ return False
58
71
59
72
ans = 0
60
73
dfs(root)
@@ -85,7 +98,6 @@ class Solution {
85
98
private int ans;
86
99
87
100
public int countUnivalSubtrees (TreeNode root ) {
88
- ans = 0 ;
89
101
dfs(root);
90
102
return ans;
91
103
}
@@ -94,19 +106,18 @@ class Solution {
94
106
if (root == null ) {
95
107
return true ;
96
108
}
97
- boolean left = dfs(root. left);
98
- boolean right = dfs(root. right);
99
- boolean t = true ;
100
- if (root. left != null && root. left. val != root. val) {
101
- t = false ;
102
- }
103
- if (root. right != null && root. right. val != root. val) {
104
- t = false ;
109
+ boolean l = dfs(root. left);
110
+ boolean r = dfs(root. right);
111
+ if (! l || ! r) {
112
+ return false ;
105
113
}
106
- if (left && t && right) {
114
+ int a = root. left == null ? root. val : root. left. val;
115
+ int b = root. right == null ? root. val : root. right. val;
116
+ if (a == b && b == root. val) {
107
117
++ ans;
118
+ return true ;
108
119
}
109
- return left && t && right ;
120
+ return false ;
110
121
}
111
122
}
112
123
```
@@ -127,24 +138,28 @@ class Solution {
127
138
*/
128
139
class Solution {
129
140
public:
130
- int ans;
131
-
132
141
int countUnivalSubtrees(TreeNode* root) {
133
- ans = 0;
142
+ int ans = 0;
143
+ function<bool(TreeNode* )> dfs = [ &] (TreeNode* root) -> bool {
144
+ if (!root) {
145
+ return true;
146
+ }
147
+ bool l = dfs(root->left);
148
+ bool r = dfs(root->right);
149
+ if (!l || !r) {
150
+ return false;
151
+ }
152
+ int a = root->left ? root->left->val : root->val;
153
+ int b = root->right ? root->right->val : root->val;
154
+ if (a == b && b == root->val) {
155
+ ++ans;
156
+ return true;
157
+ }
158
+ return false;
159
+ };
134
160
dfs(root);
135
161
return ans;
136
162
}
137
-
138
- bool dfs (TreeNode* root) {
139
- if (!root) return 1;
140
- bool left = dfs(root->left);
141
- bool right = dfs(root->right);
142
- bool t = 1;
143
- if (root->left && root->left->val != root->val) t = 0;
144
- if (root->right && root->right->val != root->val) t = 0;
145
- if (left && t && right) ++ans;
146
- return left && t && right;
147
- }
148
163
};
149
164
```
150
165
@@ -159,28 +174,27 @@ public:
159
174
* Right *TreeNode
160
175
* }
161
176
*/
162
- func countUnivalSubtrees(root *TreeNode) int {
163
- ans := 0
164
- var dfs func(root *TreeNode) bool
177
+ func countUnivalSubtrees(root *TreeNode) (ans int) {
178
+ var dfs func(*TreeNode) bool
165
179
dfs = func(root *TreeNode) bool {
166
180
if root == nil {
167
181
return true
168
182
}
169
- left, right := dfs(root.Left), dfs(root.Right)
170
- t := true
183
+ l, r := dfs(root.Left), dfs(root.Right)
184
+ if !l || !r {
185
+ return false
186
+ }
171
187
if root.Left != nil && root.Left.Val != root.Val {
172
- t = false
188
+ return false
173
189
}
174
190
if root.Right != nil && root.Right.Val != root.Val {
175
- t = false
191
+ return false
176
192
}
177
- if left && t && right {
178
- ans++
179
- }
180
- return left && t && right
193
+ ans++
194
+ return true
181
195
}
182
196
dfs(root)
183
- return ans
197
+ return
184
198
}
185
199
```
186
200
@@ -201,29 +215,71 @@ func countUnivalSubtrees(root *TreeNode) int {
201
215
*/
202
216
var countUnivalSubtrees = function (root ) {
203
217
let ans = 0 ;
204
- let dfs = function ( root ) {
218
+ const dfs = root => {
205
219
if (! root) {
206
220
return true ;
207
221
}
208
- const left = dfs (root .left ),
209
- right = dfs (root .right );
210
- let t = true ;
211
- if (root .left && root .left .val != root .val ) {
212
- t = false ;
222
+ const l = dfs (root .left );
223
+ const r = dfs (root .right );
224
+ if (! l || ! r) {
225
+ return false ;
213
226
}
214
- if (root .right && root .right .val != root .val ) {
215
- t = false ;
227
+ if (root .left && root .left .val != = root .val ) {
228
+ return false ;
216
229
}
217
- if (left && t && right ) {
218
- ++ ans ;
230
+ if (root . right && root . right . val !== root . val ) {
231
+ return false ;
219
232
}
220
- return left && t && right;
233
+ ++ ans;
234
+ return true ;
221
235
};
222
236
dfs (root);
223
237
return ans;
224
238
};
225
239
```
226
240
241
+ ### ** TypeScript**
242
+
243
+ ``` ts
244
+ /**
245
+ * Definition for a binary tree node.
246
+ * class TreeNode {
247
+ * val: number
248
+ * left: TreeNode | null
249
+ * right: TreeNode | null
250
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
251
+ * this.val = (val===undefined ? 0 : val)
252
+ * this.left = (left===undefined ? null : left)
253
+ * this.right = (right===undefined ? null : right)
254
+ * }
255
+ * }
256
+ */
257
+
258
+ function countUnivalSubtrees(root : TreeNode | null ): number {
259
+ let ans: number = 0 ;
260
+ const dfs = (root : TreeNode | null ): boolean => {
261
+ if (root == null ) {
262
+ return true ;
263
+ }
264
+ const l: boolean = dfs (root .left );
265
+ const r: boolean = dfs (root .right );
266
+ if (! l || ! r ) {
267
+ return false ;
268
+ }
269
+ if (root .left != null && root .left .val != root .val ) {
270
+ return false ;
271
+ }
272
+ if (root .right != null && root .right .val != root .val ) {
273
+ return false ;
274
+ }
275
+ ++ ans ;
276
+ return true ;
277
+ };
278
+ dfs (root );
279
+ return ans ;
280
+ }
281
+ ```
282
+
227
283
### ** ...**
228
284
229
285
```
0 commit comments