Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 287c53a

Browse files
committed
feat: add solutions to lc problems: No.0250,0572,0687
* No.0250.Count Univalue Subtrees * No.0572.Subtree of Another Tree * No.0687.Longest Univalue Path
1 parent b6f24c3 commit 287c53a

File tree

21 files changed

+741
-467
lines changed

21 files changed

+741
-467
lines changed

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

Lines changed: 98 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,24 @@
4141
# self.left = left
4242
# self.right = right
4343
class Solution:
44-
def countUnivalSubtrees(self, root: TreeNode) -> int:
45-
if root is None:
46-
return 0
47-
cnt = 0
48-
44+
def countUnivalSubtrees(self, root: Optional[TreeNode]) -> int:
4945
def dfs(root):
50-
nonlocal cnt
51-
if root.left is None and root.right is None:
52-
cnt += 1
46+
if root is None:
5347
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-
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:
56+
ans += 1
57+
return left and t and right
58+
59+
ans = 0
6460
dfs(root)
65-
return cnt
61+
return ans
6662
```
6763

6864
### **Java**
@@ -86,35 +82,31 @@ class Solution:
8682
* }
8783
*/
8884
class Solution {
89-
private int cnt;
85+
private int ans;
9086

9187
public int countUnivalSubtrees(TreeNode root) {
92-
if (root == null) {
93-
return 0;
94-
}
95-
cnt = 0;
88+
ans = 0;
9689
dfs(root);
97-
return cnt;
90+
return ans;
9891
}
9992

10093
private boolean dfs(TreeNode root) {
101-
if (root.left == null && root.right == null) {
102-
++cnt;
94+
if (root == null) {
10395
return true;
10496
}
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;
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;
109102
}
110-
if (root.right != null) {
111-
// exec dfs(root.right) first
112-
res = dfs(root.right) && res && root.val == root.right.val;
103+
if (root.right != null && root.right.val != root.val) {
104+
t = false;
113105
}
114-
if (res) {
115-
++cnt;
106+
if (left && t && right) {
107+
++ans;
116108
}
117-
return res;
109+
return left && t && right;
118110
}
119111
}
120112
```
@@ -135,27 +127,23 @@ class Solution {
135127
*/
136128
class Solution {
137129
public:
138-
int cnt;
130+
int ans;
139131

140132
int countUnivalSubtrees(TreeNode* root) {
141-
if (!root) return 0;
142-
cnt = 0;
133+
ans = 0;
143134
dfs(root);
144-
return cnt;
135+
return ans;
145136
}
146137

147138
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-
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;
159147
}
160148
};
161149
```
@@ -171,34 +159,69 @@ public:
171159
* Right *TreeNode
172160
* }
173161
*/
174-
var cnt int
175-
176162
func countUnivalSubtrees(root *TreeNode) int {
177-
if root == nil {
178-
return 0
163+
ans := 0
164+
var dfs func(root *TreeNode) bool
165+
dfs = func(root *TreeNode) bool {
166+
if root == nil {
167+
return true
168+
}
169+
left, right := dfs(root.Left), dfs(root.Right)
170+
t := true
171+
if root.Left != nil && root.Left.Val != root.Val {
172+
t = false
173+
}
174+
if root.Right != nil && root.Right.Val != root.Val {
175+
t = false
176+
}
177+
if left && t && right {
178+
ans++
179+
}
180+
return left && t && right
179181
}
180-
cnt = 0
181182
dfs(root)
182-
return cnt
183+
return ans
183184
}
185+
```
184186

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-
}
187+
### **JavaScript**
188+
189+
```js
190+
/**
191+
* Definition for a binary tree node.
192+
* function TreeNode(val, left, right) {
193+
* this.val = (val===undefined ? 0 : val)
194+
* this.left = (left===undefined ? null : left)
195+
* this.right = (right===undefined ? null : right)
196+
* }
197+
*/
198+
/**
199+
* @param {TreeNode} root
200+
* @return {number}
201+
*/
202+
var countUnivalSubtrees = function (root) {
203+
let ans = 0;
204+
let dfs = function (root) {
205+
if (!root) {
206+
return true;
207+
}
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;
213+
}
214+
if (root.right && root.right.val != root.val) {
215+
t = false;
216+
}
217+
if (left && t && right) {
218+
++ans;
219+
}
220+
return left && t && right;
221+
};
222+
dfs(root);
223+
return ans;
224+
};
202225
```
203226

204227
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /