Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45ae0f9

Browse files
Added solutions 109-115.
1 parent 7fad0ce commit 45ae0f9

File tree

14 files changed

+431
-0
lines changed

14 files changed

+431
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0101_0200.s0109_convert_sorted_list_to_binary_search_tree;
2+
3+
import com_github_leetcode.ListNode;
4+
import com_github_leetcode.TreeNode;
5+
6+
public class Solution {
7+
public TreeNode sortedListToBST(ListNode head) {
8+
// Empty list -> empty tree / sub-tree
9+
if (head == null) {
10+
return null;
11+
}
12+
13+
// No next node -> this node will become leaf
14+
if (head.next == null) {
15+
TreeNode leaf = new TreeNode(head.val);
16+
leaf.left = null;
17+
leaf.right = null;
18+
return leaf;
19+
}
20+
21+
ListNode slow = head;
22+
// Head-Start fast by 1 to get slow = mid -1
23+
ListNode fast = head.next.next;
24+
25+
// Find the mid of list
26+
while (fast != null && fast.next != null) {
27+
slow = slow.next;
28+
fast = fast.next.next;
29+
}
30+
31+
// slow.next -> mid = our "root"
32+
TreeNode root = new TreeNode(slow.next.val);
33+
34+
// Right sub tree from mid - end
35+
root.right = sortedListToBST(slow.next.next);
36+
37+
// Left sub tree from head - mid (chop slow.next)
38+
slow.next = null;
39+
root.left = sortedListToBST(head);
40+
41+
return root;
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g0101_0200.s0110_balanced_binary_tree;
2+
3+
import com_github_leetcode.TreeNode;
4+
5+
public class Solution {
6+
public boolean isBalanced(TreeNode root) {
7+
8+
// Empty Tree is balanced
9+
if (root == null) {
10+
return true;
11+
}
12+
13+
// Get max height of subtree child
14+
// Get max height of subtree child
15+
// compare height difference (cannot be more than 1)
16+
int leftHeight = 0;
17+
int rightHeight = 0;
18+
19+
if (root.left != null) {
20+
leftHeight = getMaxHeight(root.left);
21+
}
22+
23+
if (root.right != null) {
24+
rightHeight = getMaxHeight(root.right);
25+
}
26+
27+
int heightDiff = Math.abs(leftHeight - rightHeight);
28+
29+
// if passes height check
30+
// - Check if left subtree is balanced and if the right subtree is balanced
31+
// - If one of both are imbalanced, then the tree is imbalanced
32+
boolean isBalanced = false;
33+
if (heightDiff <= 1 && isBalanced(root.left) && isBalanced(root.right)) {
34+
35+
isBalanced = true;
36+
}
37+
return isBalanced;
38+
}
39+
40+
public int getMaxHeight(TreeNode root) {
41+
42+
if (root == null) {
43+
return 0;
44+
}
45+
46+
int leftHeight = 0;
47+
int rightHeight = 0;
48+
49+
// Left
50+
if (root.left != null) {
51+
leftHeight = getMaxHeight(root.left);
52+
}
53+
54+
// Right
55+
if (root.right != null) {
56+
rightHeight = getMaxHeight(root.right);
57+
}
58+
59+
if (leftHeight > rightHeight) {
60+
return 1 + leftHeight;
61+
} else {
62+
return 1 + rightHeight;
63+
}
64+
}
65+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0101_0200.s0111_minimum_depth_of_binary_tree;
2+
3+
import com_github_leetcode.TreeNode;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
public class Solution {
8+
public int minDepth(TreeNode root) {
9+
if (root == null) {
10+
return 0;
11+
}
12+
Queue<TreeNode> queue = new LinkedList<>();
13+
queue.add(root);
14+
int d = 0;
15+
while (!queue.isEmpty()) {
16+
int size = queue.size();
17+
for (int i = 0; i < size; i++) {
18+
TreeNode current = queue.poll();
19+
if (current.left == null && current.right == null && d > 0) return d + 1;
20+
if (current.right != null) queue.add(current.right);
21+
if (current.left != null) queue.add(current.left);
22+
}
23+
24+
d++;
25+
}
26+
27+
return d;
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g0101_0200.s0112_path_sum;
2+
3+
import com_github_leetcode.TreeNode;
4+
5+
public class Solution {
6+
public boolean hasPathSum(TreeNode root, int sum) {
7+
if (root == null) {
8+
return false;
9+
}
10+
if (sum == root.val && root.left == null && root.right == null) {
11+
return true;
12+
}
13+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0101_0200.s0113_path_sum_ii;
2+
3+
import com_github_leetcode.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class Solution {
8+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
9+
List<List<Integer>> res = new ArrayList<>();
10+
if (root == null) return res;
11+
recur(res, new ArrayList<>(), 0, targetSum, root);
12+
return res;
13+
}
14+
15+
private void recur(
16+
List<List<Integer>> res, ArrayList<Integer> al, int sum, int targetSum, TreeNode root) {
17+
if (root == null) {
18+
return;
19+
}
20+
al.add(root.val);
21+
sum += root.val;
22+
if (sum == targetSum && root.left == null && root.right == null) {
23+
res.add(new ArrayList<>(al));
24+
}
25+
recur(res, al, sum, targetSum, root.left);
26+
recur(res, al, sum, targetSum, root.right);
27+
al.remove(al.size() - 1);
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0101_0200.s0114_flatten_binary_tree_to_linked_list;
2+
3+
import com_github_leetcode.TreeNode;
4+
5+
public class Solution {
6+
public void flatten(TreeNode root) {
7+
if (root != null) {
8+
findTail(root);
9+
}
10+
}
11+
12+
private TreeNode findTail(TreeNode root) {
13+
TreeNode left = root.left;
14+
TreeNode right = root.right;
15+
TreeNode tail;
16+
// find the tail of left subtree, tail means the most left leaf
17+
if (left != null) {
18+
tail = findTail(left);
19+
// stitch the right subtree below the tail
20+
root.left = null;
21+
root.right = left;
22+
tail.right = right;
23+
} else {
24+
tail = root;
25+
}
26+
// find tail of the right subtree
27+
if (tail.right == null) {
28+
return tail;
29+
} else {
30+
return findTail(tail.right);
31+
}
32+
}
33+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0101_0200.s0115_distinct_subsequences;
2+
3+
public class Solution {
4+
public int numDistinct(String text, String text2) {
5+
if (text.length() < text2.length()) {
6+
return 0;
7+
}
8+
if (text.length() == text2.length()) {
9+
return (text.equals(text2) ? 1 : 0);
10+
}
11+
int move = text.length() - text2.length() + 2;
12+
// Only finite number of character in s can occupy first position in T. Same applies for
13+
// every character in T.
14+
int[] dp = new int[move];
15+
int j = 1;
16+
int k = 1;
17+
18+
for (int i = 0; i < text2.length(); i++) {
19+
boolean firstMatch = true;
20+
for (; j < move; j++) {
21+
if (text2.charAt(i) == text.charAt(i + j - 1)) {
22+
if (firstMatch) {
23+
// Keep track of first match. To avoid useless comparisons on next
24+
// iteration.
25+
k = j;
26+
firstMatch = false;
27+
}
28+
if (i == 0) {
29+
dp[j] = 1;
30+
}
31+
32+
dp[j] += dp[j - 1];
33+
} else {
34+
dp[j] = dp[j - 1];
35+
}
36+
}
37+
// No match found for current character of t in s. No point in checking others.
38+
if (dp[move - 1] == 0) {
39+
return 0;
40+
}
41+
42+
j = k;
43+
}
44+
return dp[move - 1];
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0101_0200.s0109_convert_sorted_list_to_binary_search_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void convertSortedListToBST() {
12+
ListNode nodes =
13+
new ListNode(
14+
-10, new ListNode(-3, new ListNode(0, new ListNode(5, new ListNode(9)))));
15+
assertThat(
16+
new Solution().sortedListToBST(nodes).toString(),
17+
equalTo("0,-3,-10,null,9,5,null"));
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0101_0200.s0110_balanced_binary_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void balancedBinaryTree() {
12+
TreeNode bottomLeft = new TreeNode(15);
13+
TreeNode bottomRight = new TreeNode(7);
14+
15+
TreeNode upRight = new TreeNode(20, bottomLeft, bottomRight);
16+
TreeNode upLeft = new TreeNode(9);
17+
TreeNode root = new TreeNode(3, upLeft, upRight);
18+
assertThat(new Solution().isBalanced(root), equalTo(true));
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0101_0200.s0111_minimum_depth_of_binary_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void minDepth() {
12+
TreeNode bottomLeft = new TreeNode(15);
13+
TreeNode bottomRight = new TreeNode(7);
14+
15+
TreeNode upRight = new TreeNode(20, bottomLeft, bottomRight);
16+
TreeNode upLeft = new TreeNode(9);
17+
TreeNode root = new TreeNode(3, upLeft, upRight);
18+
19+
assertThat(new Solution().minDepth(root), equalTo(2));
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /