Skip to content

Commit 04c6f41

Browse files
refactor format
1 parent ccfe2f6 commit 04c6f41

29 files changed

+313
-283
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun
2727
public class _101 {
2828
//a very natural idea flows out using recursion. Cheers.
2929
public boolean isSymmetric(TreeNode root) {
30-
if(root == null) {
30+
if (root == null) {
3131
return true;
3232
}
3333
return isSymmetric(root.left, root.right);
3434
}
3535

3636
private boolean isSymmetric(TreeNode left, TreeNode right) {
37-
if(left == null || right == null) {
37+
if (left == null || right == null) {
3838
return left == right;
3939
}
40-
if(left.val != right.val) {
40+
if (left.val != right.val) {
4141
return false;
4242
}
4343
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111
You may assume that duplicates do not exist in the tree.*/
1212
public class _105 {
1313

14-
/**credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching
15-
use HashMap as the cache so that accessing inorder index becomes O(1) time
16-
17-
Note: The first element of preorder array is the root!*/
14+
/**
15+
* credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching
16+
* use HashMap as the cache so that accessing inorder index becomes O(1) time
17+
* <p>
18+
* Note: The first element of preorder array is the root!
19+
*/
1820
public TreeNode buildTree(int[] preorder, int[] inorder) {
1921
Map<Integer, Integer> inorderMap = new HashMap();
2022
for (int i = 0; i < inorder.length; i++) {
2123
inorderMap.put(inorder[i], i);
2224
}
2325

2426
/**At the beginning, both start from 0 to nums.length-1*/
25-
return buildTree(preorder, 0, preorder.length-1, 0, inorder.length-1, inorderMap);
27+
return buildTree(preorder, 0, preorder.length - 1, 0, inorder.length - 1, inorderMap);
2628
}
2729

2830
private TreeNode buildTree(int[] preorder, int preStart, int preEnd, int inStart, int inEnd, Map<Integer, Integer> inorderMap) {
@@ -39,8 +41,8 @@ private TreeNode buildTree(int[] preorder, int preStart, int preEnd, int inStart
3941
*
4042
* since inRoot is being used already in this recursion call, that's why we use inRoot-1 and inRoot+1
4143
* this part is the same for both Leetcode 105 and Leetcode 106.*/
42-
root.left = buildTree(preorder, preStart+1, preStart+numsLeft, inStart, inRoot-1, inorderMap);
43-
root.right = buildTree(preorder, preStart+numsLeft+1, preEnd, inRoot+1, inEnd, inorderMap);
44+
root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inStart, inRoot - 1, inorderMap);
45+
root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inRoot + 1, inEnd, inorderMap);
4446
return root;
4547
}
4648

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
*/
1717
public class _106 {
1818

19-
/**https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space
20-
*
19+
/**
20+
* https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space
21+
* <p>
2122
* Note: the last element of postorder array is the root!
22-
*
23+
* <p>
2324
* The idea is to take the last element in postorder as the root; find the position of the root in the inorder array;
2425
* then locate the range for left sub-tree and right sub-tree and do recursion,
25-
* use a hashmap to record the index of root in the inorder array.*/
26+
* use a hashmap to record the index of root in the inorder array.
27+
*/
2628
public TreeNode buildTree(int[] inorder, int[] postorder) {
2729
if (inorder == null || postorder == null || inorder.length != postorder.length) {
2830
return null;
@@ -32,14 +34,14 @@ public TreeNode buildTree(int[] inorder, int[] postorder) {
3234
inorderMap.put(inorder[i], i);
3335
}
3436
/**At the beginning, both start from 0 to nums.length-1*/
35-
return buildTreeRecursively(0, inorder.length-1, postorder, 0, postorder.length-1,inorderMap);
37+
return buildTreeRecursively(0, inorder.length - 1, postorder, 0, postorder.length - 1, inorderMap);
3638
}
3739

38-
private TreeNode buildTreeRecursively(int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd, Map<Integer, Integer> inorderMap){
40+
private TreeNode buildTreeRecursively(int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd, Map<Integer, Integer> inorderMap) {
3941
if (postorderStart > postorderEnd || inorderStart > inorderEnd) return null;
4042
TreeNode root = new TreeNode(postorder[postorderEnd]);
4143
int inRoot = inorderMap.get(postorder[postorderEnd]);
42-
int numsLeft = inRoot-inorderStart;
44+
int numsLeft = inRoot - inorderStart;
4345

4446
/**It's easy to understand and remember:
4547
* for the indices of inorder array:
@@ -55,10 +57,9 @@ private TreeNode buildTreeRecursively(int inorderStart, int inorderEnd, int[] po
5557
* this is also easy to understand and remember:
5658
* since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1;
5759
* then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/
58-
root.left = buildTreeRecursively(inorderStart, inRoot-1, postorder, postorderStart, postorderStart+numsLeft-1, inorderMap);
59-
root.right = buildTreeRecursively(inRoot+1, inorderEnd, postorder, postorderStart+numsLeft, postorderEnd-1, inorderMap);
60+
root.left = buildTreeRecursively(inorderStart, inRoot - 1, postorder, postorderStart, postorderStart + numsLeft - 1, inorderMap);
61+
root.right = buildTreeRecursively(inRoot + 1, inorderEnd, postorder, postorderStart + numsLeft, postorderEnd - 1, inorderMap);
6062
return root;
6163
}
6264

63-
6465
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@
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;
38-
37+
if (root == null) return result;
38+
3939
Queue<TreeNode> q = new LinkedList();
4040
q.offer(root);
41-
while(!q.isEmpty()){
41+
while (!q.isEmpty()) {
4242
List<Integer> thisLevel = new ArrayList<Integer>();
4343
int qSize = q.size();
44-
for(int i = 0; i < qSize; i++){
44+
for (int i = 0; i < qSize; i++) {
4545
TreeNode curr = q.poll();
4646
thisLevel.add(curr.val);
47-
if(curr.left != null) q.offer(curr.left);
48-
if(curr.right != null) q.offer(curr.right);
47+
if (curr.left != null) q.offer(curr.left);
48+
if (curr.right != null) q.offer(curr.right);
4949
}
5050
result.add(thisLevel);
5151
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.*/
99

1010
public class _110 {
11-
11+
1212
class Solution1 {
1313
//recursively get the height of each subtree of each node, compare their difference, if greater than 1, then return false
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;
17+
if (root == null) return true;
18+
if (Math.abs(getH(root.left) - getH(root.right)) > 1) return false;
1919
else return isBalanced(root.left) && isBalanced(root.right);
2020
}
2121

2222
private int getH(TreeNode root) {
23-
if(root == null) return 0;//base case
23+
if (root == null) return 0;//base case
2424
int leftH = getH(root.left);
2525
int rightH = getH(root.right);
26-
return Math.max(leftH, rightH)+1;
26+
return Math.max(leftH, rightH) + 1;
2727
}
2828
}
29-
29+
3030
class Solution2 {
3131

3232
public boolean isBalanced(TreeNode root) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
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) return false;
21+
if (root.val == sum && root.left == null && root.right == null) return true;
2222
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
2323
}
2424

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,37 @@ public class _113 {
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) return allPaths;
3232
List<Integer> path = new ArrayList();
3333
dfs(root, path, allPaths, sum);
3434
return allPaths;
3535
}
3636

37-
37+
3838
private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths, int sum) {
3939
path.add(root.val);
40-
if(root.left != null){
41-
dfs(root.left, path, allPaths, sum-root.val);
40+
if (root.left != null) {
41+
dfs(root.left, path, allPaths, sum - root.val);
4242
}
43-
if(root.right != null){
44-
dfs(root.right, path, allPaths, sum-root.val);
43+
if (root.right != null) {
44+
dfs(root.right, path, allPaths, sum - root.val);
4545
}
46-
if(root.left == null && root.right == null){
47-
if(sum == root.val){
46+
if (root.left == null && root.right == null) {
47+
if (sum == root.val) {
4848
List<Integer> onePath = new ArrayList(path);
4949
allPaths.add(onePath);
5050
}
5151
}
52-
path.remove(path.size()-1);
52+
path.remove(path.size() - 1);
5353
}
5454

5555

56-
public static void main(String...strings){
56+
public static void main(String... strings) {
5757
_113 test = new _113();
5858
// TreeNode root = new TreeNode(1);
5959
// root.left = new TreeNode(2);
6060
// int sum = 1;
61-
61+
6262
// TreeNode root = new TreeNode(1);
6363
// root.left = new TreeNode(-2);
6464
// root.left.left = new TreeNode(1);
@@ -74,7 +74,7 @@ public static void main(String...strings){
7474
// 1 3 -2
7575
// /
7676
// -1
77-
77+
7878
TreeNode root = new TreeNode(5);
7979
root.left = new TreeNode(4);
8080
root.left.left = new TreeNode(11);

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ public class _115 {
1212

1313
public int numDistinct(String s, String t) {
1414
int m = s.length(), n = t.length();
15-
int[][] dp = new int[m+1][n+1];
16-
15+
int[][] dp = new int[m + 1][n + 1];
16+
1717
char[] schar = s.toCharArray();
1818
char[] tchar = t.toCharArray();
19-
20-
for(int i = 0; i <= m; i++) {
19+
20+
for (int i = 0; i <= m; i++) {
2121
dp[i][0] = 1;
2222
}
23-
24-
for(int j = 1; j <= n; j++) {
23+
24+
for (int j = 1; j <= n; j++) {
2525
dp[0][j] = 0;
2626
}
27-
28-
for(int i = 1; i <= m; i++){
29-
for(int j = 1; j <= n; j++){
30-
if(schar[i-1] == tchar[j-1]) {
31-
dp[i][j] = dp[i-1][j]+dp[i-1][j-1];
27+
28+
for (int i = 1; i <= m; i++) {
29+
for (int j = 1; j <= n; j++) {
30+
if (schar[i - 1] == tchar[j - 1]) {
31+
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
3232
} else {
33-
dp[i][j] = dp[i-1][j];
33+
dp[i][j] = dp[i - 1][j];
3434
}
3535
}
3636
}
37-
37+
3838
return dp[m][n];
3939
}
4040

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void connect(TreeLinkNode root) {
7171

7272
}
7373

74-
public static void main(String...args){
74+
public static void main(String... args) {
7575
TreeLinkNode root = new TreeLinkNode(1);
7676
root.left = new TreeLinkNode(2);
7777
root.right = new TreeLinkNode(3);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void connect(TreeLinkNode root) {
6868

6969
}
7070

71-
public static void main(String...args){
71+
public static void main(String... args) {
7272
TreeLinkNode root = new TreeLinkNode(1);
7373
root.left = new TreeLinkNode(2);
7474
root.right = new TreeLinkNode(3);

0 commit comments

Comments
 (0)