8000 refactor 250 · hiradha/Leetcode@bafccaf · GitHub
[go: up one dir, main page]

Skip to content

Commit bafccaf

Browse files
refactor 250
1 parent 439232e commit bafccaf

File tree

1 file changed

+21
-34
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+21
-34
lines changed

src/main/java/com/fishercoder/solutions/_250.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,32 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5-
/**Given a binary tree, count the number of uni-value subtrees.
6-
7-
A Uni-value subtree means all nodes of the subtree have the same value.
8-
9-
For example:
10-
Given binary tree,
11-
5
12-
/ \
13-
1 5
14-
/ \ \
15-
5 5 5
16-
return 4.
17-
18-
*/
< 10000 /td>
195
public class _250 {
206

21-
public int countUnivalSubtrees(TreeNode root) {
22-
int[] count = new int[1];
23-
helper(root, count);
24-
return count[0];
25-
}
26-
27-
private boolean helper(TreeNode node, int[] count) {
28-
if (node == null) {
29-
return true;
7+
public static class Solution1 {
8+
public int countUnivalSubtrees(TreeNode root) {
9+
int[] count = new int[1];
10+
helper(root, count);
11+
return count[0];
3012
}
31-
boolean left = helper(node.left, count);
32-
boolean right = helper(node.right, count);
33-
if (left && right) {
34-
if (node.left != null && node.val != node.left.val) {
35-
return false;
13+
14+
private boolean helper(TreeNode node, int[] count) {
15+
if (node == null) {
16+
return true;
3617
}
37-
if (node.right != null && node.val != node.right.val) {
38-
return false;
18+
boolean left = helper(node.left, count);
19+
boolean right = helper(node.right, count);
20+
if (left && right) {
21+
if (node.left != null && node.val != node.left.val) {
22+
return false;
23+
}
24+
if (node.right != null && node.val != node.right.val) {
25+
return false;
26+
}
27+
count[0]++;
28+
return true;
3929
}
40-
count[0]++;
41-
return true;
30+
return false;
4231
}
43-
return false;
4432
}
45-
4633
}

0 commit comments

Comments
 (0)
0