Skip to content

Commit 6ecd4fe

Browse files
authored
Improved tasks 101-146.
1 parent 7c306c0 commit 6ecd4fe

File tree

16 files changed

+19
-56
lines changed

16 files changed

+19
-56
lines changed

src.save/main/java/g0101_0200/s0101_symmetric_tree/Solution.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ public boolean isSymmetric(TreeNode root) {
1010
if (root == null) {
1111
return true;
1212
}
13-
1413
return helper(root.left, root.right);
1514
}
1615

1716
private boolean helper(TreeNode leftNode, TreeNode rightNode) {
1817
if (leftNode == null || rightNode == null) {
1918
return leftNode == null && rightNode == null;
2019
}
21-
2220
if (leftNode.val != rightNode.val) {
2321
return false;
2422
}
25-
2623
return helper(leftNode.left, rightNode.right) && helper(leftNode.right, rightNode.left);
2724
}
2825
}

src.save/main/java/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ public List<List<Integer>> levelOrder(TreeNode root) {
2323
root = queue.remove();
2424
while (!queue.isEmpty() && root != null) {
2525
level.add(root.val);
26-
2726
if (root.left != null) {
2827
queue.add(root.left);
2928
}
3029
if (root.right != null) {
3130
queue.add(root.right);
3231
}
33-
3432
root = queue.remove();
3533
}
3634
result.add(level);

src.save/main/java/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import java.util.Map;
99

1010
public class Solution {
11-
int j;
12-
Map<Integer, Integer> map = new HashMap<>();
11+
private int j;
12+
private Map<Integer, Integer> map = new HashMap<>();
1313

1414
public int get(int key) {
1515
return map.get(key);

src.save/main/java/g0101_0200/s0107_binary_tree_level_order_traversal_ii/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.List;
99

1010
public class Solution {
11-
List<List<Integer>> order = new ArrayList<>();
11+
private List<List<Integer>> order = new ArrayList<>();
1212

1313
public List<List<Integer>> levelOrderBottom(TreeNode root) {
1414
getOrder(root, 0);

src.save/main/java/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/Solution.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@ public TreeNode sortedArrayToBST(int[] num) {
1010
return makeTree(num, 0, num.length - 1);
1111
}
1212

13-
public TreeNode makeTree(int[] num, int left, int right) {
13+
private TreeNode makeTree(int[] num, int left, int right) {
1414
/*2. left as lowest# can't be greater than right*/
1515
if (left > right) {
1616
return null;
1717
}
18-
1918
/*3. Set median# as node*/
2019
int mid = (left + right) / 2;
2120
TreeNode midNode = new TreeNode(num[mid]);
22-
2321
/*4. Set mid node's kids*/
2422
midNode.left = makeTree(num, left, mid - 1);
2523
midNode.right = makeTree(num, mid + 1, right);
26-
2724
/*5. Sends node back || Goes back to prev node*/
2825
return midNode;
2926
}

src.save/main/java/g0101_0200/s0109_convert_sorted_list_to_binary_search_tree/Solution.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,28 @@ public TreeNode sortedListToBST(ListNode head) {
1111
if (head == null) {
1212
return null;
1313
}
14-
1514
// No next node -> this node will become leaf
1615
if (head.next == null) {
1716
TreeNode leaf = new TreeNode(head.val);
1817
leaf.left = null;
1918
leaf.right = null;
2019
return leaf;
2120
}
22-
2321
ListNode slow = head;
2422
// Head-Start fast by 1 to get slow = mid -1
2523
ListNode fast = head.next.next;
26-
2724
// Find the mid of list
2825
while (fast != null && fast.next != null) {
2926
slow = slow.next;
3027
fast = fast.next.next;
3128
}
32-
3329
// slow.next -> mid = our "root"
3430
TreeNode root = new TreeNode(slow.next.val);
35-
3631
// Right sub tree from mid - end
3732
root.right = sortedListToBST(slow.next.next);
38-
3933
// Left sub tree from head - mid (chop slow.next)
4034
slow.next = null;
4135
root.left = sortedListToBST(head);
42-
4336
return root;
4437
}
4538
}

src.save/main/java/g0101_0200/s0110_balanced_binary_tree/Solution.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,42 @@
66

77
public class Solution {
88
public boolean isBalanced(TreeNode root) {
9-
109
// Empty Tree is balanced
1110
if (root == null) {
1211
return true;
1312
}
14-
1513
// Get max height of subtree child
1614
// Get max height of subtree child
1715
// compare height difference (cannot be more than 1)
1816
int leftHeight = 0;
1917
int rightHeight = 0;
20-
2118
if (root.left != null) {
2219
leftHeight = getMaxHeight(root.left);
2320
}
24-
2521
if (root.right != null) {
2622
rightHeight = getMaxHeight(root.right);
2723
}
28-
2924
int heightDiff = Math.abs(leftHeight - rightHeight);
30-
3125
// if passes height check
3226
// - Check if left subtree is balanced and if the right subtree is balanced
3327
// - If one of both are imbalanced, then the tree is imbalanced
3428
return heightDiff <= 1 && isBalanced(root.left) && isBalanced(root.right);
3529
}
3630

37-
public int getMaxHeight(TreeNode root) {
38-
31+
private int getMaxHeight(TreeNode root) {
3932
if (root == null) {
4033
return 0;
4134
}
42-
4335
int leftHeight = 0;
4436
int rightHeight = 0;
45-
4637
// Left
4738
if (root.left != null) {
4839
leftHeight = getMaxHeight(root.left);
4940
}
50-
5141
// Right
5242
if (root.right != null) {
5343
rightHeight = getMaxHeight(root.right);
5444
}
55-
5645
if (leftHeight > rightHeight) {
5746
return 1 + leftHeight;
5847
} else {

src.save/main/java/g0101_0200/s0115_distinct_subsequences/Solution.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public int numDistinct(String text, String text2) {
1616
int[] dp = new int[move];
1717
int j = 1;
1818
int k = 1;
19-
2019
for (int i = 0; i < text2.length(); i++) {
2120
boolean firstMatch = true;
2221
for (; j < move; j++) {
@@ -40,7 +39,6 @@ public int numDistinct(String text, String text2) {
4039
if (dp[move - 1] == 0) {
4140
return 0;
4241
}
43-
4442
j = k;
4543
}
4644
return dp[move - 1];

src.save/main/java/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/Solution.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ public Node connect(Node root) {
4343
root.left.next = adjacentRightNode(root.next);
4444
}
4545
}
46-
4746
if (root.right != null) {
4847
root.right.next = adjacentRightNode(root.next);
4948
}
50-
5149
connect(root.right);
5250
connect(root.left);
5351
return root;

src.save/main/java/g0101_0200/s0118_pascals_triangle/Solution.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
public class Solution {
1010
public List<List<Integer>> generate(int numRows) {
1111
List<List<Integer>> output = new ArrayList<>();
12-
1312
for (int i = 0; i < numRows; i++) {
1413
List<Integer> currRow = new ArrayList<>();
1514
for (int j = 0; j <= i; j++) {

0 commit comments

Comments
 (0)