Skip to content

Commit f032520

Browse files
refactor 965
1 parent eaac0cb commit f032520

File tree

1 file changed

+16
-51
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+16
-51
lines changed

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

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,23 @@
22

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

5-
/**
6-
* 965. Univalued Binary Tree
7-
*
8-
* A binary tree is univalued if every node in the tree has the same value.
9-
*
10-
* Return true if and only if the given tree is univalued.
11-
*
12-
* Example 1:
13-
*
14-
* 1
15-
* / \
16-
* 1 1
17-
* / \ \
18-
* 1 1 1
19-
*
20-
* Input: [1,1,1,1,1,null,1]
21-
* Output: true
22-
*
23-
*
24-
* Example 2:
25-
* 2
26-
* / \
27-
* 2 2
28-
* / \
29-
* 5 2
30-
*
31-
* Input: [2,2,2,5,2]
32-
* Output: false
33-
*
34-
*
35-
* Note:
36-
*
37-
* The number of nodes in the given tree will be in the range [1, 100].
38-
* Each node's value will be an integer in the range [0, 99].
39-
*/
405
public class _965 {
41-
public static class Solution1 {
42-
public boolean isUnivalTree(TreeNode root) {
43-
if (root == null) {
44-
return true;
45-
}
46-
return dfs(root, root.val);
47-
}
6+
public static class Solution1 {
7+
public boolean isUnivalTree(TreeNode root) {
8+
if (root == null) {
9+
return true;
10+
}
11+
return dfs(root, root.val);
12+
}
4813

49-
private boolean dfs(TreeNode root, int value) {
50-
if (root == null) {
51-
return true;
52-
}
53-
if (root.val != value) {
54-
return false;
55-
}
56-
return dfs(root.left, value) && dfs(root.right, value);
14+
private boolean dfs(TreeNode root, int value) {
15+
if (root == null) {
16+
return true;
17+
}
18+
if (root.val != value) {
19+
return false;
20+
}
21+
return dfs(root.left, value) && dfs(root.right, value);
22+
}
5723
}
58-
}
5924
}

0 commit comments

Comments
 (0)