Skip to content

Commit b35275e

Browse files
committedOct 5, 2021
feat: add solutions to lc problems: No.0250,0572
1 parent c1b6ae8 commit b35275e

File tree

12 files changed

+782
-48
lines changed

12 files changed

+782
-48
lines changed
 

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

+157-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<strong>输出:</strong> 4
2424
</pre>
2525

26-
2726
## 解法
2827

2928
<!-- 这里可写通用的实现逻辑 -->
@@ -35,15 +34,171 @@
3534
<!-- 这里可写当前语言的特殊实现逻辑 -->
3635

3736
```python
38-
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, val=0, left=None, right=None):
40+
# self.val = val
41+
# self.left = left
42+
# self.right = right
43+
class Solution:
44+
def countUnivalSubtrees(self, root: TreeNode) -> int:
45+
if root is None:
46+
return 0
47+
cnt = 0
48+
49+
def dfs(root):
50+
nonlocal cnt
51+
if root.left is None and root.right is None:
52+
cnt += 1
53+
return True
54+
res = True
55+
if root.left:
56+
# exec dfs(root.left) first
57+
res = dfs(root.left) and res and root.val == root.left.val
58+
if root.right:
59+
# exec dfs(root.right) first
60+
res = dfs(root.right) and res and root.val == root.right.val
61+
cnt += res
62+
return res
63+
64+
dfs(root)
65+
return cnt
3966
```
4067

4168
### **Java**
4269

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

4572
```java
73+
/**
74+
* Definition for a binary tree node.
75+
* public class TreeNode {
76+
* int val;
77+
* TreeNode left;
78+
* TreeNode right;
79+
* TreeNode() {}
80+
* TreeNode(int val) { this.val = val; }
81+
* TreeNode(int val, TreeNode left, TreeNode right) {
82+
* this.val = val;
83+
* this.left = left;
84+
* this.right = right;
85+
* }
86+
* }
87+
*/
88+
class Solution {
89+
private int cnt;
90+
91+
public int countUnivalSubtrees(TreeNode root) {
92+
if (root == null) {
93+
return 0;
94+
}
95+
cnt = 0;
96+
dfs(root);
97+
return cnt;
98+
}
99+
100+
private boolean dfs(TreeNode root) {
101+
if (root.left == null && root.right == null) {
102+
++cnt;
103+
return true;
104+
}
105+
boolean res = true;
106+
if (root.left != null) {
107+
// exec dfs(root.left) first
108+
res = dfs(root.left) && res && root.val == root.left.val;
109+
}
110+
if (root.right != null) {
111+
// exec dfs(root.right) first
112+
res = dfs(root.right) && res && root.val == root.right.val;
113+
}
114+
if (res) {
115+
++cnt;
116+
}
117+
return res;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
/**
126+
* Definition for a binary tree node.
127+
* struct TreeNode {
128+
* int val;
129+
* TreeNode *left;
130+
* TreeNode *right;
131+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
132+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
133+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
134+
* };
135+
*/
136+
class Solution {
137+
public:
138+
int cnt;
139+
140+
int countUnivalSubtrees(TreeNode* root) {
141+
if (!root) return 0;
142+
cnt = 0;
143+
dfs(root);
144+
return cnt;
145+
}
146+
147+
bool dfs(TreeNode* root) {
148+
if (!root->left && !root->right)
149+
{
150+
++cnt;
151+
return true;
152+
}
153+
bool res = true;
154+
if (root->left) res = dfs(root->left) && res && root->val == root->left->val;
155+
if (root->right) res = dfs(root->right) && res && root->val == root->right->val;
156+
cnt += res;
157+
return res;
158+
159+
}
160+
};
161+
```
46162
163+
### **Go**
164+
165+
```go
166+
/**
167+
* Definition for a binary tree node.
168+
* type TreeNode struct {
169+
* Val int
170+
* Left *TreeNode
171+
* Right *TreeNode
172+
* }
173+
*/
174+
var cnt int
175+
176+
func countUnivalSubtrees(root *TreeNode) int {
177+
if root == nil {
178+
return 0
179+
}
180+
cnt = 0
181+
dfs(root)
182+
return cnt
183+
}
184+
185+
func dfs(root *TreeNode) bool {
186+
if root.Left == nil && root.Right == nil {
187+
cnt++
188+
return true
189+
}
190+
res := true
191+
if root.Left != nil {
192+
res = dfs(root.Left) && res && root.Val == root.Left.Val
193+
}
194+
if root.Right != nil {
195+
res = dfs(root.Right) && res && root.Val == root.Right.Val
196+
}
197+
if res {
198+
cnt++
199+
}
200+
return res
201+
}
47202
```
48203

49204
### **...**

‎solution/0200-0299/0250.Count Univalue Subtrees/README_EN.md

+157-2
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,176 @@
3838
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
3939
</ul>
4040

41-
4241
## Solutions
4342

4443
<!-- tabs:start -->
4544

4645
### **Python3**
4746

4847
```python
49-
48+
# Definition for a binary tree node.
49+
# class TreeNode:
50+
# def __init__(self, val=0, left=None, right=None):
51+
# self.val = val
52+
# self.left = left
53+
# self.right = right
54+
class Solution:
55+
def countUnivalSubtrees(self, root: TreeNode) -> int:
56+
if root is None:
57+
return 0
58+
cnt = 0
59+
60+
def dfs(root):
61+
nonlocal cnt
62+
if root.left is None and root.right is None:
63+
cnt += 1
64+
return True
65+
res = True
66+
if root.left:
67+
# exec dfs(root.left) first
68+
res = dfs(root.left) and res and root.val == root.left.val
69+
if root.right:
70+
# exec dfs(root.right) first
71+
res = dfs(root.right) and res and root.val == root.right.val
72+
cnt += res
73+
return res
74+
75+
dfs(root)
76+
return cnt
5077
```
5178

5279
### **Java**
5380

5481
```java
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode() {}
89+
* TreeNode(int val) { this.val = val; }
90+
* TreeNode(int val, TreeNode left, TreeNode right) {
91+
* this.val = val;
92+
* this.left = left;
93+
* this.right = right;
94+
* }
95+
* }
96+
*/
97+
class Solution {
98+
private int cnt;
99+
100+
public int countUnivalSubtrees(TreeNode root) {
101+
if (root == null) {
102+
return 0;
103+
}
104+
cnt = 0;
105+
dfs(root);
106+
return cnt;
107+
}
108+
109+
private boolean dfs(TreeNode root) {
110+
if (root.left == null && root.right == null) {
111+
++cnt;
112+
return true;
113+
}
114+
boolean res = true;
115+
if (root.left != null) {
116+
// exec dfs(root.left) first
117+
res = dfs(root.left) && res && root.val == root.left.val;
118+
}
119+
if (root.right != null) {
120+
// exec dfs(root.right) first
121+
res = dfs(root.right) && res && root.val == root.right.val;
122+
}
123+
if (res) {
124+
++cnt;
125+
}
126+
return res;
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
/**
135+
* Definition for a binary tree node.
136+
* struct TreeNode {
137+
* int val;
138+
* TreeNode *left;
139+
* TreeNode *right;
140+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
141+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
143+
* };
144+
*/
145+
class Solution {
146+
public:
147+
int cnt;
148+
149+
int countUnivalSubtrees(TreeNode* root) {
150+
if (!root) return 0;
151+
cnt = 0;
152+
dfs(root);
153+
return cnt;
154+
}
155+
156+
bool dfs(TreeNode* root) {
157+
if (!root->left && !root->right)
158+
{
159+
++cnt;
160+
return true;
161+
}
162+
bool res = true;
163+
if (root->left) res = dfs(root->left) && res && root->val == root->left->val;
164+
if (root->right) res = dfs(root->right) && res && root->val == root->right->val;
165+
cnt += res;
166+
return res;
167+
168+
}
169+
};
170+
```
55171
172+
### **Go**
173+
174+
```go
175+
/**
176+
* Definition for a binary tree node.
177+
* type TreeNode struct {
178+
* Val int
179+
* Left *TreeNode
180+
* Right *TreeNode
181+
* }
182+
*/
183+
var cnt int
184+
185+
func countUnivalSubtrees(root *TreeNode) int {
186+
if root == nil {
187+
return 0
188+
}
189+
cnt = 0
190+
dfs(root)
191+
return cnt
192+
}
193+
194+
func dfs(root *TreeNode) bool {
195+
if root.Left == nil && root.Right == nil {
196+
cnt++
197+
return true
198+
}
199+
res := true
200+
if root.Left != nil {
201+
res = dfs(root.Left) && res && root.Val == root.Left.Val
202+
}
203+
if root.Right != nil {
204+
res = dfs(root.Right) && res && root.Val == root.Right.Val
205+
}
206+
if res {
207+
cnt++
208+
}
209+
return res
210+
}
56211
```
57212

58213
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 cnt;
15+
16+
int countUnivalSubtrees(TreeNode* root) {
17+
if (!root) return 0;
18+
cnt = 0;
19+
dfs(root);
20+
return cnt;
21+
}
22+
23+
bool dfs(TreeNode* root) {
24+
if (!root->left && !root->right)
25+
{
26+
++cnt;
27+
return true;
28+
}
29+
bool res = true;
30+
if (root->left) res = dfs(root->left) && res && root->val == root->left->val;
31+
if (root->right) res = dfs(root->right) && res && root->val == root->right->val;
32+
cnt += res;
33+
return res;
34+
35+
}
36+
};

0 commit comments

Comments
 (0)
Please sign in to comment.