Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 67c2415

Browse files
refactor
1 parent f055d19 commit 67c2415

File tree

3 files changed

+49
-54
lines changed

3 files changed

+49
-54
lines changed

fishercoder_codestyle.xml

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
<!-- Checks for whitespace -->
3030
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
31-
<!--<module name="FileTabCharacter">
32-
<property name="eachLine" value="false"/>
33-
</module>-->
31+
<!--this cannot be enabled because right now Intellij CE cannot import my fishercoder_codestyle.xml as -->
32+
<!--my chosen Java code style-->
33+
<!--<module name="FileTabCharacter">-->
34+
<!--<property name="eachLine" value="false"/>-->
35+
<!--</module>-->
3436

3537
<!--<module name="SuppressionCommentFilter">-->
3638
<!--<property name="offCommentFormat" value="CHECKSTYLE_OFF\: ([\w\|]+)"/>-->
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
*
3-
*/
41
package com.fishercoder.common.utils;
52

63
import java.lang.annotation.Retention;
@@ -9,17 +6,17 @@
96
@Retention(RetentionPolicy.SOURCE)
107
public @interface Notes {
118

12-
String todo() default "";
9+
String todo() default "";
1310

14-
String problemReview() default "";
11+
String problemReview() default "";
1512

16-
String issue() default "";
13+
String issue() default "";
1714

18-
String context() default ""; // this variable is used to state how I solved
19-
// this problem, whether completely made it
20-
// myself, or copied it from online, or a
21-
// combination of both approaches.
15+
String context() default ""; // this variable is used to state how I solved
16+
// this problem, whether completely made it
17+
// myself, or copied it from online, or a
18+
// combination of both approaches.
2219

23-
boolean needsReview() default true;
20+
boolean needsReview() default true;
2421

2522
}

src/main/java/com/fishercoder/common/utils/TreeUtils.java

+36-40
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,51 @@
77

88
import java.util.*;
99

10-
1110
/**
1211
* This is a util class to contain all tree related methods.
1312
*/
1413
public class TreeUtils {
15-
16-
/**
17-
* This method is to construct a normal binary tree. The input reads like
18-
* this for [5, 3, 6, 2, 4, null, null, 1]:
19-
20-
5
21-
/ \
22-
3 6
23-
/ \ / \
24-
2 4 # #
25-
/
26-
1
27-
28-
*/
29-
@Notes(context = "This is usually how Leetcode OJ passes a binary tree into testing: "
30-
+ "https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying"
31-
+ "the test case from Leetcode in the form of [1, null, 2, 3].")
32-
public static TreeNode constructBinaryTree(List<Integer> treeValues) {
33-
TreeNode root = new TreeNode(treeValues.get(0));
34-
Queue<TreeNode> queue = new LinkedList<>();
35-
queue.offer(root);
36-
for (int i = 1; i < treeValues.size(); i++) {
37-
TreeNode curr = queue.poll();
38-
if (treeValues.get(i) != null) {
39-
curr.left = new TreeNode(treeValues.get(i));
40-
queue.offer(curr.left);
41-
}
42-
if (++i < treeValues.size() && treeValues.get(i) != null) {
43-
curr.right = new TreeNode(treeValues.get(i));
44-
queue.offer(curr.right);
45-
}
14+
/**
15+
* This method is to construct a normal binary tree. The input reads like
16+
* this for [5, 3, 6, 2, 4, null, null, 1]:
17+
5
18+
/ \
19+
3 6
20+
/ \ / \
21+
2 4 # #
22+
/
23+
1
24+
*/
25+
@Notes(context = "This is usually how Leetcode OJ passes a binary tree into testing: "
26+
+ "https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying"
27+
+ "the test case from Leetcode in the form of [1, null, 2, 3].")
28+
public static TreeNode constructBinaryTree(List<Integer> treeValues) {
29+
TreeNode root = new TreeNode(treeValues.get(0));
30+
Queue<TreeNode> queue = new LinkedList<>();
31+
queue.offer(root);
32+
for (int i = 1; i < treeValues.size(); i++) {
33+
TreeNode curr = queue.poll();
34+
if (treeValues.get(i) != null) {
35+
curr.left = new TreeNode(treeValues.get(i));
36+
queue.offer(curr.left);
37+
}
38+
if (++i < treeValues.size() && treeValues.get(i) != null) {
39+
curr.right = new TreeNode(treeValues.get(i));
40+
queue.offer(curr.right);
4641
}
47-
return root;
4842
}
49-
43+
return root;
44+
}
45+
5046
public static void printBinaryTree(TreeNode root) {
5147
CommonUtils.println("\nPrinting out the binary tree in a very visual manner as below:\n");
52-
48+
5349
// imitating from BTreePrinter class
5450
int maxLevel = TreeUtils.maxLevel(root);
5551

5652
printNodeInternal(Collections.singletonList(root), 1, maxLevel);
5753
}
58-
54+
5955
private static int maxLevel(TreeNode root) {
6056
if (root == null) {
6157
return 0;
@@ -64,7 +60,7 @@ private static int maxLevel(TreeNode root) {
6460
return Math.max(TreeUtils.maxLevel(root.left),
6561
TreeUtils.maxLevel(root.right)) + 1;
6662
}
67-
63+
6864
private static void printNodeInternal(
6965
List<TreeNode> list, int level, int maxLevel) {
7066
if (list.isEmpty() || CommonUtils.isAllElementsNull(list))
@@ -122,13 +118,13 @@ private static void printNodeInternal(
122118

123119
printNodeInternal(newNodes, level + 1, maxLevel);
124120
}
125-
126-
public static void inOrderTraversal(TreeNode root){
121+
122+
public static void inOrderTraversal(TreeNode root) {
127123
inOrder(root);
128124
}
129125

130126
private static void inOrder(TreeNode root) {
131-
if(root == null) {
127+
if (root == null) {
132128
return;
133129
}
134130
inOrder(root.left);

0 commit comments

Comments
 (0)