Skip to content

Commit 3ed0f8a

Browse files
authored
Added tasks 111, 112, 113, 115.
1 parent 78c4e27 commit 3ed0f8a

File tree

13 files changed

+428
-3
lines changed

13 files changed

+428
-3
lines changed

Diff for: README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
977977

978978
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
979979
|-|-|-|-|-|-
980-
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 267 | 24.51
980+
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 208 | 72.24
981981
| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 290 | 26.98
982982
| 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 194 | 92.89
983983

@@ -1114,8 +1114,9 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
11141114
| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/Solution.kt)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 316 | 34.25
11151115
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/Solution.kt)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 334 | 35.39
11161116
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 307 | 43.93
1117-
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 267 | 24.51
1117+
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 208 | 72.24
11181118
| 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 233 | 54.90
1119+
| 0111 |[Minimum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 525 | 90.51
11191120
| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
11201121
| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 398 | 12.89
11211122
| 0124 |[Binary Tree Maximum Path Sum](src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 331 | 74.42
@@ -1249,6 +1250,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
12491250
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12501251
|-|-|-|-|-|-
12511252
| 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 233 | 54.90
1253+
| 0112 |[Path Sum](src/main/kotlin/g0101_0200/s0112_path_sum/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 322 | 36.41
12521254

12531255
#### Day 13 Tree
12541256

@@ -1367,6 +1369,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
13671369
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13681370
|-|-|-|-|-|-
13691371
| 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 194 | 92.89
1372+
| 0113 |[Path Sum II](src/main/kotlin/g0101_0200/s0113_path_sum_ii/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Backtracking | 364 | 78.67
13701373

13711374
#### Day 17 Tree
13721375

@@ -1539,7 +1542,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15391542
| 0128 |[Longest Consecutive Sequence](src/main/kotlin/g0101_0200/s0128_longest_consecutive_sequence/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find | 460 | 97.77
15401543
| 0124 |[Binary Tree Maximum Path Sum](src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue | 331 | 74.42
15411544
| 0121 |[Best Time to Buy and Sell Stock](src/main/kotlin/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays | 609 | 94.06
1545+
| 0115 |[Distinct Subsequences](src/main/kotlin/g0101_0200/s0115_distinct_subsequences/Solution.kt)| Hard | String, Dynamic_Programming | 285 | 88.89
15421546
| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List | 191 | 93.10
1547+
| 0113 |[Path Sum II](src/main/kotlin/g0101_0200/s0113_path_sum_ii/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Backtracking, Data_Structure_II_Day_16_Tree | 364 | 78.67
1548+
| 0112 |[Path Sum](src/main/kotlin/g0101_0200/s0112_path_sum/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree | 322 | 36.41
1549+
| 0111 |[Minimum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue | 525 | 90.51
15431550
| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Programming_Skills_II_Day_2, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue | 398 | 12.89
15441551
| 0109 |[Convert Sorted List to Binary Search Tree](src/main/kotlin/g0101_0200/s0109_convert_sorted_list_to_binary_search_tree/Solution.kt)| Medium | Tree, Binary_Tree, Linked_List, Binary_Search_Tree, Divide_and_Conquer | 376 | 59.26
15451552
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/kotlin/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/Solution.kt)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Level_2_Day_9_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 334 | 35.39
@@ -1550,7 +1557,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15501557
| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/kotlin/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/Solution.kt)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_15_Tree, Udemy_Tree_Stack_Queue | 316 | 34.25
15511558
| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue | 355 | 29.37
15521559
| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree | 290 | 26.98
1553-
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Level_2_Day_15_Tree, Udemy_Tree_Stack_Queue | 267 | 24.51
1560+
| 0100 |[Same Tree](src/main/kotlin/g0001_0100/s0100_same_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Level_2_Day_15_Tree, Udemy_Tree_Stack_Queue | 208 | 72.24
15541561
| 0099 |[Recover Binary Search Tree](src/main/kotlin/g0001_0100/s0099_recover_binary_search_tree/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 492 | 39.39
15551562
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 330 | 41.38
15561563
| 0097 |[Interleaving String](src/main/kotlin/g0001_0100/s0097_interleaving_string/Solution.kt)| Medium | String, Dynamic_Programming | 240 | 57.50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0101_0200.s0111_minimum_depth_of_binary_tree
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Udemy_Tree_Stack_Queue
4+
// #2022_09_29_Time_525_ms_(90.51%)_Space_55.3_MB_(90.51%)
5+
6+
import com_github_leetcode.TreeNode
7+
import java.util.LinkedList
8+
import java.util.Queue
9+
10+
/*
11+
* Example:
12+
* var ti = TreeNode(5)
13+
* var v = ti.`val`
14+
* Definition for a binary tree node.
15+
* class TreeNode(var `val`: Int) {
16+
* var left: TreeNode? = null
17+
* var right: TreeNode? = null
18+
* }
19+
*/
20+
class Solution {
21+
fun minDepth(root: TreeNode?): Int {
22+
if (root == null) {
23+
return 0
24+
}
25+
val queue: Queue<TreeNode> = LinkedList()
26+
queue.add(root)
27+
var d = 0
28+
while (!queue.isEmpty()) {
29+
val size: Int = queue.size
30+
for (i in 0 until size) {
31+
val current: TreeNode = queue.poll()
32+
if (current.left == null && current.right == null && d > 0) {
33+
return d + 1
34+
}
35+
if (current.right != null) {
36+
queue.add(current.right)
37+
}
38+
if (current.left != null) {
39+
queue.add(current.left)
40+
}
41+
}
42+
d++
43+
}
44+
return d
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
111\. Minimum Depth of Binary Tree
2+
3+
Easy
4+
5+
Given a binary tree, find its minimum depth.
6+
7+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
8+
9+
**Note:** A leaf is a node with no children.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg)
14+
15+
**Input:** root = [3,9,20,null,null,15,7]
16+
17+
**Output:** 2
18+
19+
**Example 2:**
20+
21+
**Input:** root = [2,null,3,null,4,null,5,null,6]
22+
23+
**Output:** 5
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range <code>[0, 10<sup>5</sup>]</code>.
28+
* `-1000 <= Node.val <= 1000`
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0101_0200.s0112_path_sum
2+
3+
import com_github_leetcode.TreeNode
4+
5+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Data_Structure_I_Day_12_Tree
6+
// #2022_09_29_Time_322_ms_(36.41%)_Space_39.6_MB_(18.97%)
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun hasPathSum(root: TreeNode?, sum: Int): Boolean {
20+
if (root == null) {
21+
return false
22+
}
23+
return if (sum == root.`val` && root.left == null && root.right == null) {
24+
true
25+
} else hasPathSum(root.left, sum - root.`val`) || hasPathSum(root.right, sum - root.`val`)
26+
}
27+
}

Diff for: src/main/kotlin/g0101_0200/s0112_path_sum/readme.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
112\. Path Sum
2+
3+
Easy
4+
5+
Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
6+
7+
A **leaf** is a node with no children.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg)
12+
13+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
14+
15+
**Output:** true
16+
17+
**Explanation:** The root-to-leaf path with the target sum is shown.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg)
22+
23+
**Input:** root = [1,2,3], targetSum = 5
24+
25+
**Output:** false
26+
27+
**Explanation:** There two root-to-leaf paths in the tree:
28+
29+
(1 --> 2): The sum is 3.
30+
31+
(1 --> 3): The sum is 4.
32+
33+
There is no root-to-leaf path with sum = 5.
34+
35+
**Example 3:**
36+
37+
**Input:** root = [], targetSum = 0
38+
39+
**Output:** false
40+
41+
**Explanation:** Since the tree is empty, there are no root-to-leaf paths.
42+
43+
**Constraints:**
44+
45+
* The number of nodes in the tree is in the range `[0, 5000]`.
46+
* `-1000 <= Node.val <= 1000`
47+
* `-1000 <= targetSum <= 1000`
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0101_0200.s0113_path_sum_ii
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree #Backtracking #Data_Structure_II_Day_16_Tree
4+
// #2022_09_29_Time_364_ms_(78.67%)_Space_39.7_MB_(83.93%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
@Suppress("NAME_SHADOWING")
19+
class Solution {
20+
fun pathSum(root: TreeNode?, targetSum: Int): List<List<Int>> {
21+
val res: MutableList<List<Int>> = ArrayList()
22+
if (root == null) {
23+
return res
24+
}
25+
recur(res, ArrayList(), 0, targetSum, root)
26+
return res
27+
}
28+
29+
private fun recur(
30+
res: MutableList<List<Int>>,
31+
al: ArrayList<Int>,
32+
sum: Int,
33+
targetSum: Int,
34+
root: TreeNode?
35+
) {
36+
var sum = sum
37+
if (root == null) {
38+
return
39+
}
40+
al.add(root.`val`)
41+
sum += root.`val`
42+
if (sum == targetSum && root.left == null && root.right == null) {
43+
res.add(ArrayList(al))
44+
}
45+
recur(res, al, sum, targetSum, root.left)
46+
recur(res, al, sum, targetSum, root.right)
47+
al.removeAt(al.size - 1)
48+
}
49+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
113\. Path Sum II
2+
3+
Medium
4+
5+
Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
6+
7+
A **root-to-leaf** path is a path starting from the root and ending at any leaf node. A **leaf** is a node with no children.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg)
12+
13+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
14+
15+
**Output:** [[5,4,11,2],[5,8,4,5]]
16+
17+
**Explanation:** There are two paths whose sum equals targetSum:
18+
19+
5 + 4 + 11 + 2 = 22
20+
21+
5 + 8 + 4 + 5 = 22
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg)
26+
27+
**Input:** root = [1,2,3], targetSum = 5
28+
29+
**Output:** []
30+
31+
**Example 3:**
32+
33+
**Input:** root = [1,2], targetSum = 0
34+
35+
**Output:** []
36+
37+
**Constraints:**
38+
39+
* The number of nodes in the tree is in the range `[0, 5000]`.
40+
* `-1000 <= Node.val <= 1000`
41+
* `-1000 <= targetSum <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0101_0200.s0115_distinct_subsequences
2+
3+
// #Hard #String #Dynamic_Programming #2022_09_29_Time_285_ms_(88.89%)_Space_34.2_MB_(100.00%)
4+
5+
class Solution {
6+
fun numDistinct(text: String, text2: String): Int {
7+
if (text.length < text2.length) {
8+
return 0
9+
}
10+
if (text.length == text2.length) {
11+
return if (text == text2) 1 else 0
12+
}
13+
val move = text.length - text2.length + 2
14+
// Only finite number of character in s can occupy first position in T. Same applies for
15+
// every character in T.
16+
val dp = IntArray(move)
17+
var j = 1
18+
var k = 1
19+
for (i in 0 until text2.length) {
20+
var firstMatch = true
21+
while (j < move) {
22+
if (text2[i] == text[i + j - 1]) {
23+
if (firstMatch) {
24+
// Keep track of first match. To avoid useless comparisons on next
25+
// iteration.
26+
k = j
27+
firstMatch = false
28+
}
29+
if (i == 0) {
30+
dp[j] = 1
31+
}
32+
dp[j] += dp[j - 1]
33+
} else {
34+
dp[j] = dp[j - 1]
35+
}
36+
j++
37+
}
38+
// No match found for current character of t in s. No point in checking others.
39+
if (dp[move - 1] == 0) {
40+
return 0
41+
}
42+
j = k
43+
}
44+
return dp[move - 1]
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
115\. Distinct Subsequences
2+
3+
Hard
4+
5+
Given two strings `s` and `t`, return _the number of distinct subsequences of `s` which equals `t`_.
6+
7+
A string's **subsequence** is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining characters' relative positions. (i.e., `"ACE"` is a subsequence of `"ABCDE"` while `"AEC"` is not).
8+
9+
The test cases are generated so that the answer fits on a 32-bit signed integer.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "rabbbit", t = "rabbit"
14+
15+
**Output:** 3
16+
17+
**Explanation:** As shown below, there are 3 ways you can generate "rabbit" from s. <code>**<ins>rabb</ins>**b**<ins>it</ins>**</code> <code>**<ins>ra</ins>**b**<ins>bbit</ins>**</code> <code>**<ins>rab</ins>**b**<ins>bit</ins>**</code>
18+
19+
**Example 2:**
20+
21+
**Input:** s = "babgbag", t = "bag"
22+
23+
**Output:** 5
24+
25+
**Explanation:** As shown below, there are 5 ways you can generate "bag" from s. <code>**<ins>ba</ins>**b<ins>**g**</ins>bag</code> <code>**<ins>ba</ins>**bgba**<ins>g</ins>**</code> <code><ins>**b**</ins>abgb**<ins>ag</ins>**</code> <code>ba<ins>**b**</ins>gb<ins>**ag**</ins></code> <code>babg**<ins>bag</ins>**</code>
26+
27+
**Constraints:**
28+
29+
* `1 <= s.length, t.length <= 1000`
30+
* `s` and `t` consist of English letters.

0 commit comments

Comments
 (0)