8000 enable NeedBraces check! · IsaacVictor/Leetcode@66d146a · GitHub
[go: up one dir, main page]

Skip to content

Commit 66d146a

Browse files
enable NeedBraces check!
1 parent a1d5dc2 commit 66d146a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+399
-149
lines changed

fishercoder_checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<property name="tokens" value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
7575
</module>
7676

77-
<!--<module name="NeedBraces"/>-->
77+
<module name="NeedBraces"/>
7878

7979
<module name="LeftCurly">
8080
<property name="maxLineLength" value="100"/>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
public class _104 {
1111

1212
public int maxDepth(TreeNode root) {
13-
if (root == null) return 0;
13+
if (root == null) {
14+
return 0;
15+
}
1416
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
1517
}
1618

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public TreeNode buildTree(int[] preorder, int[] inorder) {
2828
}
2929

3030
private TreeNode buildTree(int[] preorder, int preStart, int preEnd, int inStart, int inEnd, Map<Integer, Integer> inorderMap) {
31-
if (preStart > preEnd || inStart > inEnd) return null;
31+
if (preStart > preEnd || inStart > inEnd) {
32+
return null;
33+
}
3234

3335
TreeNode root = new TreeNode(preorder[preStart]);
3436
int inRoot = inorderMap.get(preorder[preStart]);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
public class _107 {
3535
public List<List<Integer>> levelOrder(TreeNode root) {
3636
List<List<Integer>> result = new ArrayList();
37-
if (root == null) return result;
37+
if (root == null) {
38+
return result;
39+
}
3840

3941
Queue<TreeNode> q = new LinkedList();
4042
q.offer(root);
@@ -44,8 +46,12 @@ public List<List<Integer>> levelOrder(TreeNode root) {
4446
for (int i = 0; i < qSize; i++) {
4547
TreeNode curr = q.poll();
4648
thisLevel.add(curr.val);
47-
if (curr.left != null) q.offer(curr.left);
48-
if (curr.right != null) q.offer(curr.right);
49+
if (curr.left != null) {
50+
q.offer(curr.left);
51+
}
52+
if (curr.right != null) {
53+
q.offer(curr.right);
54+
}
4955
}
5056
result.add(thisLevel);
5157
}

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ class Solution1 {
1414
//although this is working, but it's not efficient, since it repeatedly computes the heights of each node every time
1515
//Its time complexity is O(n^2).
1616
public boolean isBalanced(TreeNode root) {
17-
if (root == null) return true;
18-
if (Math.abs(getH(root.left) - getH(root.right)) > 1) return false;
19-
else return isBalanced(root.left) && isBalanced(root.right);
17+
if (root == null) {
18+
return true;
19+
}
20+
if (Math.abs(getH(root.left) - getH(root.right)) > 1) {
21+
return false;
22+
} else {
23+
return isBalanced(root.left) && isBalanced(root.right);
24+
}
2025
}
2126

2227
private int getH(TreeNode root) {
23-
if (root == null) return 0;//base case
28+
if (root == null) {
29+
return 0;//base case
30+
}
2431
int leftH = getH(root.left);
2532
int rightH = getH(root.right);
2633
return Math.max(leftH, rightH) + 1;
@@ -34,12 +41,20 @@ public boolean isBalanced(TreeNode root) {
3441
}
3542

3643
private int getH(TreeNode root) {
37-
if (root == null) return 0;
44+
if (root == null) {
45+
return 0;
46+
}
3847
int leftH = getH(root.left);
39-
if (leftH == -1) return -1;
48+
if (leftH == -1) {
49+
return -1;
50+
}
4051
int rightH = getH(root.right);
41-
if (rightH == -1) return -1;
42-
if (Math.abs(leftH - rightH) > 1) return -1;
52+
if (rightH == -1) {
53+
return -1;
54+
}
55+
if (Math.abs(leftH - rightH) > 1) {
56+
return -1;
57+
}
4358
return Math.max(leftH, rightH) + 1;
4459
}
4560
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ public class _111 {
2121
public static class DFSSolution {
2222

2323
public int minDepth(TreeNode root) {
24-
if (root == null) return 0;
24+
if (root == null) {
25+
return 0;
26+
}
2527
int left = minDepth(root.left);
2628
int right = minDepth(root.right);
27-
if (left == 0) return right + 1;
28-
if (right == 0) return left + 1;
29+
if (left == 0) {
30+
return right + 1;
31+
}
32+
if (right == 0) {
33+
return left + 1;
34+
}
2935
return Math.min(left, right) + 1;
3036
}
3137

@@ -34,7 +40,9 @@ public int minDepth(TreeNode root) {
3440
public static class BFSSolution {
3541

3642
public int minDepth_BFS(TreeNode root) {
37-
if (root == null) return 0;
43+
if (root == null) {
44+
return 0;
45+
}
3846
Queue<TreeNode> q = new LinkedList();
3947
q.offer(root);
4048
int level = 0;
@@ -43,9 +51,15 @@ public int minDepth_BFS(TreeNode root) {
4351
int size = q.size();
4452
for (int i = 0; i < size; i++) {
4553
TreeNode curr = q.poll();
46-
if (curr.left != null) q.offer(curr.left);
47-
if (curr.right != null) q.offer(curr.right);
48-
if (curr.left == null && curr.right == null) return level;
54+
if (curr.left != null) {
55+
q.offer(curr.left);
56+
}
57+
if (curr.right != null) {
58+
q.offer(curr.right);
59+
}
60+
if (curr.left == null && curr.right == null) {
61+
return level;
62+
}
4963
}
5064
}
5165
return level;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.*/
1818
public class _112 {
1919
public boolean hasPathSum(TreeNode root, int sum) {
20-
if (root == null) return false;
21-
if (root.val == sum && root.left == null && root.right == null) return true;
20+
if (root == null) {
21+
return false;
22+
}
23+
if (root.val == sum && root.left == null && root.right == null) {
24+
return true;
25+
}
2226
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
2327
}
2428

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
2828
//also, it's possible that a node's value could be negative, as long as the sum of root to leaf ends up to sum
2929
public List<List<Integer>> pathSum(TreeNode root, int sum) {
3030
List<List<Integer>> allPaths = new ArrayList();
31-
if (root == null) return allPaths;
31+
if (root == null) {
32+
return allPaths;
33+
}
3234
List<Integer> path = new ArrayList();
3335
dfs(root, path, allPaths, sum);
3436
return allPaths;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class _119 {
1919

2020
public static class Solution1 {
2121
public List<Integer> getRow(int rowIndex) {
22-
if (rowIndex < 0) return new ArrayList();
22+
if (rowIndex < 0) {
23+
return new ArrayList();
24+
}
2325
List<List<Integer>> result = new ArrayList();
2426
List<Integer> row = new ArrayList();
2527
row.add(1);

0 commit comments

Comments
 (0)
0