Skip to content

Commit deed1c5

Browse files
authored
Added tasks 94-124
1 parent 73254ed commit deed1c5

File tree

33 files changed

+968
-0
lines changed

33 files changed

+968
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
377377

378378
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
379379
|-|-|-|-|-|-
380+
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
380381

381382
#### Day 8
382383

@@ -398,6 +399,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
398399

399400
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
400401
|-|-|-|-|-|-
402+
| 0096 |[Unique Binary Search Trees](src/main/scala/g0001_0100/s0096_unique_binary_search_trees/Solution.scala)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(1) | 403 | 66.67
401403

402404
#### Day 12
403405

@@ -504,6 +506,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
504506

505507
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
506508
|-|-|-|-|-|-
509+
| 0104 |[Maximum Depth of Binary Tree](src/main/scala/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 472 | 90.83
507510

508511
#### Day 11 Containers and Libraries
509512

@@ -771,11 +774,13 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
771774

772775
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
773776
|-|-|-|-|-|-
777+
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
774778

775779
#### Day 6 Tree
776780

777781
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
778782
|-|-|-|-|-|-
783+
| 0102 |[Binary Tree Level Order Traversal](src/main/scala/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 522 | 83.33
779784

780785
#### Day 7 Binary Search
781786

@@ -786,6 +791,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
786791

787792
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
788793
|-|-|-|-|-|-
794+
| 0098 |[Validate Binary Search Tree](src/main/scala/g0001_0100/s0098_validate_binary_search_tree/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 507 | 70.21
789795

790796
#### Day 9 Graph/BFS/DFS
791797

@@ -906,6 +912,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
906912

907913
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
908914
|-|-|-|-|-|-
915+
| 0101 |[Symmetric Tree](src/main/scala/g0101_0200/s0101_symmetric_tree/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 454 | 90.38
909916

910917
#### Day 16 Design
911918

@@ -961,6 +968,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
961968

962969
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
963970
|-|-|-|-|-|-
971+
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
964972
| 0001 |[Two Sum](src/main/scala/g0001_0100/s0001_two_sum/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 517 | 86.66
965973
| 0055 |[Jump Game](src/main/scala/g0001_0100/s0055_jump_game/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 622 | 88.31
966974
| 0075 |[Sort Colors](src/main/scala/g0001_0100/s0075_sort_colors/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 450 | 84.21
@@ -997,6 +1005,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
9971005

9981006
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9991007
|-|-|-|-|-|-
1008+
| 0114 |[Flatten Binary Tree to Linked List](src/main/scala/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.scala)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 491 | 54.17
10001009
| 0024 |[Swap Nodes in Pairs](src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/Solution.scala)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 441 | 95.65
10011010
| 0021 |[Merge Two Sorted Lists](src/main/scala/g0001_0100/s0021_merge_two_sorted_lists/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 480 | 89.72
10021011
| 0025 |[Reverse Nodes in k-Group](src/main/scala/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.scala)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 520 | 80.00
@@ -1005,6 +1014,11 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10051014

10061015
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10071016
|-|-|-|-|-|-
1017+
| 0094 |[Binary Tree Inorder Traversal](src/main/scala/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 456 | 68.42
1018+
| 0102 |[Binary Tree Level Order Traversal](src/main/scala/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 522 | 83.33
1019+
| 0104 |[Maximum Depth of Binary Tree](src/main/scala/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 472 | 90.83
1020+
| 0124 |[Binary Tree Maximum Path Sum](src/main/scala/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 523 | 89.47
1021+
| 0098 |[Validate Binary Search Tree](src/main/scala/g0001_0100/s0098_validate_binary_search_tree/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 507 | 70.21
10081022

10091023
#### Udemy Trie and Heap
10101024

@@ -1063,6 +1077,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10631077

10641078
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10651079
|-|-|-|-|-|-
1080+
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
10661081

10671082
#### Day 4 Array
10681083

@@ -1101,11 +1116,15 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
11011116

11021117
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11031118
|-|-|-|-|-|-
1119+
| 0094 |[Binary Tree Inorder Traversal](src/main/scala/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 456 | 68.42
11041120

11051121
#### Day 11 Tree
11061122

11071123
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11081124
|-|-|-|-|-|-
1125+
| 0102 |[Binary Tree Level Order Traversal](src/main/scala/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 522 | 83.33
1126+
| 0104 |[Maximum Depth of Binary Tree](src/main/scala/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 472 | 90.83
1127+
| 0101 |[Symmetric Tree](src/main/scala/g0101_0200/s0101_symmetric_tree/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 454 | 90.38
11091128

11101129
#### Day 12 Tree
11111130

@@ -1121,6 +1140,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
11211140

11221141
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11231142
|-|-|-|-|-|-
1143+
| 0098 |[Validate Binary Search Tree](src/main/scala/g0001_0100/s0098_validate_binary_search_tree/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 507 | 70.21
11241144

11251145
### Data Structure II
11261146

@@ -1207,6 +1227,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
12071227

12081228
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12091229
|-|-|-|-|-|-
1230+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/scala/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 564 | 91.67
12101231

12111232
#### Day 16 Tree
12121233

@@ -1320,6 +1341,16 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
13201341

13211342
| # | Title | Difficulty | Tag | Time, ms | Time, %
13221343
|------|----------------|-------------|-------------|----------|---------
1344+
| 0124 |[Binary Tree Maximum Path Sum](src/main/scala/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 523 | 89.47
1345+
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.scala)| 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, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
1346+
| 0114 |[Flatten Binary Tree to Linked List](src/main/scala/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.scala)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 491 | 54.17
1347+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/scala/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 564 | 91.67
1348+
| 0104 |[Maximum Depth of Binary Tree](src/main/scala/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(H) | 472 | 90.83
1349+
| 0102 |[Binary Tree Level Order Traversal](src/main/scala/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.scala)| 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, Big_O_Time_O(N)_Space_O(N) | 522 | 83.33
1350+
| 0101 |[Symmetric Tree](src/main/scala/g0101_0200/s0101_symmetric_tree/Solution.scala)| 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, Big_O_Time_O(N)_Space_O(log(N)) | 454 | 90.38
1351+
| 0098 |[Validate Binary Search Tree](src/main/scala/g0001_0100/s0098_validate_binary_search_tree/Solution.scala)| 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, Big_O_Time_O(N)_Space_O(log(N)) | 507 | 70.21
1352+
| 0096 |[Unique Binary Search Trees](src/main/scala/g0001_0100/s0096_unique_binary_search_trees/Solution.scala)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 403 | 66.67
1353+
| 0094 |[Binary Tree Inorder Traversal](src/main/scala/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 456 | 68.42
13231354
| 0084 |[Largest Rectangle in Histogram](src/main/scala/g0001_0100/s0084_largest_rectangle_in_histogram/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n_log_n)_Space_O(log_n) | 904 | 71.43
13241355
| 0079 |[Word Search](src/main/scala/g0001_0100/s0079_word_search/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Big_O_Time_O(4^(m\*n))_Space_O(m\*n) | 783 | 94.87
13251356
| 0078 |[Subsets](src/main/scala/g0001_0100/s0078_subsets/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 452 | 87.50
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com_github_leetcode
2+
3+
import scala.collection.mutable
4+
5+
class TreeNode(var value: Int, var left: TreeNode = null, var right: TreeNode = null) {
6+
override def toString: String = {
7+
if (left == null && right == null) {
8+
value.toString
9+
} else {
10+
val root = value.toString
11+
val leftValue = Option(left).map(_.toString).getOrElse("null")
12+
val rightValue = Option(right).map(_.toString).getOrElse("null")
13+
s"$root,$leftValue,$rightValue"
14+
}
15+
}
16+
}
17+
18+
object TreeNode {
19+
def create(treeValues: List[Option[Int]]): TreeNode = {
20+
if (treeValues.isEmpty) {
21+
null
22+
} else {
23+
val root = new TreeNode(treeValues.head.getOrElse(0))
24+
val queue = mutable.Queue(root)
25+
var i = 1
26+
while (i < treeValues.length) {
27+
val curr = queue.dequeue()
28+
if (treeValues(i).isDefined) {
29+
curr.left = new TreeNode(treeValues(i).get)
30+
queue.enqueue(curr.left)
31+
}
32+
i += 1
33+
if (i < treeValues.length && treeValues(i).isDefined) {
34+
curr.right = new TreeNode(treeValues(i).get)
35+
queue.enqueue(curr.right)
36+
}
37+
i += 1
38+
}
39+
root
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0001_0100.s0094_binary_tree_inorder_traversal
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
4+
// #Stack #Data_Structure_I_Day_10_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
5+
// #2023_11_03_Time_456_ms_(68.42%)_Space_57.3_MB_(7.89%)
6+
7+
import com_github_leetcode.TreeNode
8+
import scala.collection.mutable.ListBuffer
9+
10+
/*
11+
* Definition for a binary tree node.
12+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
13+
* var value: Int = _value
14+
* var left: TreeNode = _left
15+
* var right: TreeNode = _right
16+
* }
17+
*/
18+
object Solution {
19+
def inorderTraversal(root: TreeNode): List[Int] = {
20+
if (root == null) {
21+
List.empty
22+
} else {
23+
val answer = ListBuffer[Int]()
24+
inorderTraversal(root, answer)
25+
answer.toList
26+
}
27+
}
28+
29+
private def inorderTraversal(root: TreeNode, answer: ListBuffer[Int]): Unit = {
30+
if (root == null) {
31+
return
32+
}
33+
if (root.left != null) {
34+
inorderTraversal(root.left, answer)
35+
}
36+
answer += root.value
37+
if (root.right != null) {
38+
inorderTraversal(root.right, answer)
39+
}
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
94\. Binary Tree Inorder Traversal
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
10+
11+
**Input:** root = [1,null,2,3]
12+
13+
**Output:** [1,3,2]
14+
15+
**Example 2:**
16+
17+
**Input:** root = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** root = [1]
24+
25+
**Output:** [1]
26+
27+
**Example 4:**
28+
29+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)
30+
31+
**Input:** root = [1,2]
32+
33+
**Output:** [2,1]
34+
35+
**Example 5:**
36+
37+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)
38+
39+
**Input:** root = [1,null,2]
40+
41+
**Output:** [1,2]
42+
43+
**Constraints:**
44+
45+
* The number of nodes in the tree is in the range `[0, 100]`.
46+
* `-100 <= Node.val <= 100`
47+
48+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0096_unique_binary_search_trees
2+
3+
// #Medium #Top_100_Liked_Questions #Dynamic_Programming #Math #Tree #Binary_Tree
4+
// #Binary_Search_Tree #Dynamic_Programming_I_Day_11 #Big_O_Time_O(n)_Space_O(1)
5+
// #2023_11_03_Time_403_ms_(66.67%)_Space_51.5_MB_(100.00%)
6+
7+
object Solution {
8+
def numTrees(n: Int): Int = {
9+
var result: Long = 1L
10+
for (i <- 0 until n) {
11+
result *= 2L * n - i
12+
result /= i + 1
13+
}
14+
result /= (n + 1)
15+
result.toInt
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
96\. Unique Binary Search Trees
2+
3+
Medium
4+
5+
Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg)
10+
11+
**Input:** n = 3
12+
13+
**Output:** 5
14+
15+
**Example 2:**
16+
17+
**Input:** n = 1
18+
19+
**Output:** 1
20+
21+
**Constraints:**
22+
23+
* `1 <= n <= 19`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0001_0100.s0098_validate_binary_search_tree
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
4+
// #Binary_Search_Tree #Data_Structure_I_Day_14_Tree #Level_1_Day_8_Binary_Search_Tree
5+
// #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(log(N))
6+
// #2023_11_03_Time_507_ms_(70.21%)_Space_57_MB_(93.62%)
7+
8+
import com_github_leetcode.TreeNode
9+
10+
/*
11+
* Definition for a binary tree node.
12+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
13+
* var value: Int = _value
14+
* var left: TreeNode = _left
15+
* var right: TreeNode = _right
16+
* }
17+
*/
18+
object Solution {
19+
def isValidBST(root: TreeNode): Boolean = {
20+
def solve(node: TreeNode, left: Long, right: Long): Boolean = {
21+
if (node == null) {
22+
true
23+
} else if (node.value <= left || node.value >= right) {
24+
false
25+
} else {
26+
solve(node.left, left, node.value) && solve(node.right, node.value, right)
27+
}
28+
}
29+
30+
solve(root, Long.MinValue, Long.MaxValue)
31+
}
32+
}

0 commit comments

Comments
 (0)