Skip to content

Commit e966b25

Browse files
committed
feat: add solutions to lc problem: No.0250
No.0250.Count Univalue Subtrees
1 parent 0338ae8 commit e966b25

File tree

8 files changed

+308
-171
lines changed

8 files changed

+308
-171
lines changed

solution/0200-0299/0250.Count Univalue Subtrees/README.md

+113-57
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727

2828
<!-- 这里可写通用的实现逻辑 -->
2929

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+
3043
<!-- tabs:start -->
3144

3245
### **Python3**
@@ -45,16 +58,16 @@ class Solution:
4558
def dfs(root):
4659
if root is None:
4760
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
5668
ans += 1
57-
return left and t and right
69+
return True
70+
return False
5871

5972
ans = 0
6073
dfs(root)
@@ -85,7 +98,6 @@ class Solution {
8598
private int ans;
8699

87100
public int countUnivalSubtrees(TreeNode root) {
88-
ans = 0;
89101
dfs(root);
90102
return ans;
91103
}
@@ -94,19 +106,18 @@ class Solution {
94106
if (root == null) {
95107
return true;
96108
}
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;
105113
}
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) {
107117
++ans;
118+
return true;
108119
}
109-
return left && t && right;
120+
return false;
110121
}
111122
}
112123
```
@@ -127,24 +138,28 @@ class Solution {
127138
*/
128139
class Solution {
129140
public:
130-
int ans;
131-
132141
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+
};
134160
dfs(root);
135161
return ans;
136162
}
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-
}
148163
};
149164
```
150165
@@ -159,28 +174,27 @@ public:
159174
* Right *TreeNode
160175
* }
161176
*/
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
165179
dfs = func(root *TreeNode) bool {
166180
if root == nil {
167181
return true
168182
}
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+
}
171187
if root.Left != nil && root.Left.Val != root.Val {
172-
t = false
188+
return false
173189
}
174190
if root.Right != nil && root.Right.Val != root.Val {
175-
t = false
191+
return false
176192
}
177-
if left && t && right {
178-
ans++
179-
}
180-
return left && t && right
193+
ans++
194+
return true
181195
}
182196
dfs(root)
183-
return ans
197+
return
184198
}
185199
```
186200

@@ -201,29 +215,71 @@ func countUnivalSubtrees(root *TreeNode) int {
201215
*/
202216
var countUnivalSubtrees = function (root) {
203217
let ans = 0;
204-
let dfs = function (root) {
218+
const dfs = root => {
205219
if (!root) {
206220
return true;
207221
}
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;
213226
}
214-
if (root.right && root.right.val != root.val) {
215-
t = false;
227+
if (root.left && root.left.val !== root.val) {
228+
return false;
216229
}
217-
if (left && t && right) {
218-
++ans;
230+
if (root.right && root.right.val !== root.val) {
231+
return false;
219232
}
220-
return left && t && right;
233+
++ans;
234+
return true;
221235
};
222236
dfs(root);
223237
return ans;
224238
};
225239
```
226240

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+
227283
### **...**
228284

229285
```

0 commit comments

Comments
 (0)