Skip to content

Commit

Permalink
Added tasks 94-124
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Nov 3, 2023
1 parent 73254ed commit deed1c5
Show file tree
Hide file tree
Showing 33 changed files with 968 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 8

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 12

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 11 Containers and Libraries

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 6 Tree

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 7 Binary Search

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 9 Graph/BFS/DFS

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 16 Design

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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
| 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
Expand Down Expand Up @@ -997,6 +1005,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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
| 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
Expand All @@ -1005,6 +1014,11 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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
| 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
| 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

#### Udemy Trie and Heap

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 4 Array

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 11 Tree

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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

#### Day 12 Tree

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

### Data Structure II

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 16 Tree

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

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|---------
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
Expand Down
42 changes: 42 additions & 0 deletions src/main/scala/com_github_leetcode/TreeNode.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com_github_leetcode

import scala.collection.mutable

class TreeNode(var value: Int, var left: TreeNode = null, var right: TreeNode = null) {
override def toString: String = {
if (left == null && right == null) {
value.toString
} else {
val root = value.toString
val leftValue = Option(left).map(_.toString).getOrElse("null")
val rightValue = Option(right).map(_.toString).getOrElse("null")
s"$root,$leftValue,$rightValue"
}
}
}

object TreeNode {
def create(treeValues: List[Option[Int]]): TreeNode = {
if (treeValues.isEmpty) {
null
} else {
val root = new TreeNode(treeValues.head.getOrElse(0))
val queue = mutable.Queue(root)
var i = 1
while (i < treeValues.length) {
val curr = queue.dequeue()
if (treeValues(i).isDefined) {
curr.left = new TreeNode(treeValues(i).get)
queue.enqueue(curr.left)
}
i += 1
if (i < treeValues.length && treeValues(i).isDefined) {
curr.right = new TreeNode(treeValues(i).get)
queue.enqueue(curr.right)
}
i += 1
}
root
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g0001_0100.s0094_binary_tree_inorder_traversal

// #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)
// #2023_11_03_Time_456_ms_(68.42%)_Space_57.3_MB_(7.89%)

import com_github_leetcode.TreeNode
import scala.collection.mutable.ListBuffer

/*
* Definition for a binary tree node.
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
* var value: Int = _value
* var left: TreeNode = _left
* var right: TreeNode = _right
* }
*/
object Solution {
def inorderTraversal(root: TreeNode): List[Int] = {
if (root == null) {
List.empty
} else {
val answer = ListBuffer[Int]()
inorderTraversal(root, answer)
answer.toList
}
}

private def inorderTraversal(root: TreeNode, answer: ListBuffer[Int]): Unit = {
if (root == null) {
return
}
if (root.left != null) {
inorderTraversal(root.left, answer)
}
answer += root.value
if (root.right != null) {
inorderTraversal(root.right, answer)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
94\. Binary Tree Inorder Traversal

Easy

Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)

**Input:** root = [1,null,2,3]

**Output:** [1,3,2]

**Example 2:**

**Input:** root = []

**Output:** []

**Example 3:**

**Input:** root = [1]

**Output:** [1]

**Example 4:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)

**Input:** root = [1,2]

**Output:** [2,1]

**Example 5:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)

**Input:** root = [1,null,2]

**Output:** [1,2]

**Constraints:**

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`

**Follow up:** Recursive solution is trivial, could you do it iteratively?
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g0001_0100.s0096_unique_binary_search_trees

// #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)
// #2023_11_03_Time_403_ms_(66.67%)_Space_51.5_MB_(100.00%)

object Solution {
def numTrees(n: Int): Int = {
var result: Long = 1L
for (i <- 0 until n) {
result *= 2L * n - i
result /= i + 1
}
result /= (n + 1)
result.toInt
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
96\. Unique Binary Search Trees

Medium

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`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg)

**Input:** n = 3

**Output:** 5

**Example 2:**

**Input:** n = 1

**Output:** 1

**Constraints:**

* `1 <= n <= 19`
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g0001_0100.s0098_validate_binary_search_tree

// #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))
// #2023_11_03_Time_507_ms_(70.21%)_Space_57_MB_(93.62%)

import com_github_leetcode.TreeNode

/*
* Definition for a binary tree node.
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
* var value: Int = _value
* var left: TreeNode = _left
* var right: TreeNode = _right
* }
*/
object Solution {
def isValidBST(root: TreeNode): Boolean = {
def solve(node: TreeNode, left: Long, right: Long): Boolean = {
if (node == null) {
true
} else if (node.value <= left || node.value >= right) {
false
} else {
solve(node.left, left, node.value) && solve(node.right, node.value, right)
}
}

solve(root, Long.MinValue, Long.MaxValue)
}
}
Loading

0 comments on commit deed1c5

Please sign in to comment.