Skip to content

Commit bd87a27

Browse files
authored
Added tasks 107, 108, 109, 110.
1 parent 5c73a3f commit bd87a27

File tree

13 files changed

+442
-0
lines changed

13 files changed

+442
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
560560

561561
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
562562
|-|-|-|-|-|-
563+
| 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
563564

564565
#### Day 3
565566

@@ -916,6 +917,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
916917
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
917918
|-|-|-|-|-|-
918919
| 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
920+
| 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
919921

920922
#### Day 7 Tree
921923

@@ -935,6 +937,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
935937

936938
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
937939
|-|-|-|-|-|-
940+
| 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
938941
| 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 393 | 33.33
939942

940943
#### Day 10 Graph/BFS/DFS
@@ -1109,10 +1112,12 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
11091112
| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 269 | 38.80
11101113
| 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 | 355 | 29.37
11111114
| 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
1115+
| 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
11121116
| 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
11131117
| 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
11141118
| 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
11151119
| 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
1120+
| 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
11161121
| 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
11171122
| 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 | 330 | 41.38
11181123
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree | 386 | 45.21
@@ -1353,6 +1358,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
13531358

13541359
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13551360
|-|-|-|-|-|-
1361+
| 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
13561362
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 370 | 58.31
13571363
| 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
13581364

@@ -1534,6 +1540,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15341540
| 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
15351541
| 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
15361542
| 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
1543+
| 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
1544+
| 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
1545+
| 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
1546+
| 0107 |[Binary Tree Level Order Traversal II](src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/Solution.kt)| Medium | Breadth_First_Search, Tree, Binary_Tree | 409 | 6.82
15371547
| 0106 |[Construct Binary Tree from Inorder and Postorder Traversal](src/main/kotlin/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/Solution.kt)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 358 | 61.29
15381548
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree | 370 | 58.31
15391549
| 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, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue | 236 | 83.39
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0101_0200.s0107_binary_tree_level_order_traversal_ii
2+
3+
// #Medium #Breadth_First_Search #Tree #Binary_Tree
4+
// #2022_09_28_Time_409_ms_(6.82%)_Space_36.6_MB_(72.73%)
5+
6+
import com_github_leetcode.TreeNode
7+
import java.util.Collections
8+
import kotlin.collections.ArrayList
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+
private val order: MutableList<MutableList<Int>> = ArrayList()
22+
fun levelOrderBottom(root: TreeNode?): List<MutableList<Int>> {
23+
getOrder(root, 0)
24+
Collections.reverse(order)
25+
return order
26+
}
27+
28+
fun getOrder(root: TreeNode?, level: Int) {
29+
if (root != null) {
30+
if (level + 1 > order.size) {
31+
order.add(ArrayList())
32+
}
33+
order[level].add(root.`val`)
34+
getOrder(root.left, level + 1)
35+
getOrder(root.right, level + 1)
36+
}
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
107\. Binary Tree Level Order Traversal II
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
10+
11+
**Input:** root = [3,9,20,null,null,15,7]
12+
13+
**Output:** [[15,7],[9,20],[3]]
14+
15+
**Example 2:**
16+
17+
**Input:** root = [1]
18+
19+
**Output:** [[1]]
20+
21+
**Example 3:**
22+
23+
**Input:** root = []
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range `[0, 2000]`.
30+
* `-1000 <= Node.val <= 1000`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0101_0200.s0108_convert_sorted_array_to_binary_search_tree
2+
3+
// #Easy #Top_Interview_Questions #Array #Tree #Binary_Tree #Binary_Search_Tree #Divide_and_Conquer
4+
// #Data_Structure_II_Day_15_Tree #Level_2_Day_9_Binary_Search_Tree #Udemy_Tree_Stack_Queue
5+
// #2022_09_28_Time_334_ms_(35.39%)_Space_36.9_MB_(92.05%)
6+
7+
import com_github_leetcode.TreeNode
8+
9+
/**
10+
* Example:
11+
* var ti = TreeNode(5)
12+
* var v = ti.`val`
13+
* Definition for a binary tree node.
14+
* class TreeNode(var `val`: Int) {
15+
* var left: TreeNode? = null
16+
* var right: TreeNode? = null
17+
* }
18+
*/
19+
class Solution {
20+
fun sortedArrayToBST(num: IntArray): TreeNode? {
21+
/*1. Set up recursion*/
22+
return makeTree(num, 0, num.size - 1)
23+
}
24+
25+
private fun makeTree(num: IntArray, left: Int, right: Int): TreeNode? {
26+
/*2. left as lowest# can't be greater than right*/
27+
if (left > right) {
28+
return null
29+
}
30+
/*3. Set median# as node*/
31+
val mid = (left + right) / 2
32+
val midNode = TreeNode(num[mid])
33+
/*4. Set mid node's kids*/
34+
midNode.left = makeTree(num, left, mid - 1)
35+
midNode.right = makeTree(num, mid + 1, right)
36+
/*5. Sends node back || Goes back to prev node*/
37+
return midNode
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
108\. Convert Sorted Array to Binary Search Tree
2+
3+
Easy
4+
5+
Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a **height-balanced** binary search tree_.
6+
7+
A **height-balanced** binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg)
12+
13+
**Input:** nums = [-10,-3,0,5,9]
14+
15+
**Output:** [0,-3,9,-10,null,5]
16+
17+
**Explanation:** [0,-10,5,null,-3,null,9] is also accepted: ![](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg)
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg)
22+
23+
**Input:** nums = [1,3]
24+
25+
**Output:** [3,1]
26+
27+
**Explanation:** [1,null,3] and [3,1] are both height-balanced BSTs.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
32+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
33+
* `nums` is sorted in a **strictly increasing** order.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0101_0200.s0109_convert_sorted_list_to_binary_search_tree
2+
3+
// #Medium #Tree #Binary_Tree #Linked_List #Binary_Search_Tree #Divide_and_Conquer
4+
// #2022_09_28_Time_376_ms_(59.26%)_Space_43.3_MB_(66.67%)
5+
6+
import com_github_leetcode.ListNode
7+
import com_github_leetcode.TreeNode
8+
9+
/*
10+
* Example:
11+
* var li = ListNode(5)
12+
* var v = li.`val`
13+
* Definition for singly-linked list.
14+
* class ListNode(var `val`: Int) {
15+
* var next: ListNode? = null
16+
* }
17+
*/
18+
/*
19+
* Example:
20+
* var ti = TreeNode(5)
21+
* var v = ti.`val`
22+
* Definition for a binary tree node.
23+
* class TreeNode(var `val`: Int) {
24+
* var left: TreeNode? = null
25+
* var right: TreeNode? = null
26+
* }
27+
*/
28+
class Solution {
29+
fun sortedListToBST(head: ListNode?): TreeNode? {
30+
// Empty list -> empty tree / sub-tree
31+
if (head == null) {
32+
return null
33+
}
34+
// No next node -> this node will become leaf
35+
if (head.next == null) {
36+
val leaf = TreeNode(head.`val`)
37+
leaf.left = null
38+
leaf.right = null
39+
return leaf
40+
}
41+
var slow = head
42+
// Head-Start fast by 1 to get slow = mid -1
43+
var fast = head.next!!.next
44+
// Find the mid of list
45+
while (fast != null && fast.next != null) {
46+
slow = slow!!.next
47+
fast = fast.next!!.next
48+
}
49+
// slow.next -> mid = our "root"
50+
val root = TreeNode(slow!!.next!!.`val`)
51+
// Right sub tree from mid - end
52+
root.right = sortedListToBST(slow.next!!.next)
53+
// Left sub tree from head - mid (chop slow.next)
54+
slow.next = null
55+
root.left = sortedListToBST(head)
56+
return root
57+
}
58+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
109\. Convert Sorted List to Binary Search Tree
2+
3+
Medium
4+
5+
Given the `head` of a singly linked list where elements are **sorted in ascending order**, convert it to a height balanced BST.
6+
7+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of _every_ node never differ by more than 1.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/08/17/linked.jpg)
12+
13+
**Input:** head = [-10,-3,0,5,9]
14+
15+
**Output:** [0,-3,9,-10,null,5]
16+
17+
**Explanation:** One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
18+
19+
**Example 2:**
20+
21+
**Input:** head = []
22+
23+
**Output:** []
24+
25+
**Constraints:**
26+
27+
* The number of nodes in `head` is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.
28+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g0101_0200.s0110_balanced_binary_tree
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree #Programming_Skills_II_Day_2 #Level_2_Day_6_Tree
4+
// #Udemy_Tree_Stack_Queue #2022_09_28_Time_398_ms_(12.89%)_Space_43.8_MB_(12.58%)
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+
class Solution {
19+
fun isBalanced(root: TreeNode?): Boolean {
20+
// Empty Tree is balanced
21+
if (root == null) {
22+
return true
23+
}
24+
// Get max height of subtree child
25+
// Get max height of subtree child
26+
// compare height difference (cannot be more than 1)
27+
var leftHeight = 0
28+
var rightHeight = 0
29+
if (root.left != null) {
30+
leftHeight = getMaxHeight(root.left)
31+
}
32+
if (root.right != null) {
33+
rightHeight = getMaxHeight(root.right)
34+
}
35+
val heightDiff = Math.abs(leftHeight - rightHeight)
36+
// if passes height check
37+
// - Check if left subtree is balanced and if the right subtree is balanced
38+
// - If one of both are imbalanced, then the tree is imbalanced
39+
return heightDiff <= 1 && isBalanced(root.left) && isBalanced(root.right)
40+
}
41+
42+
private fun getMaxHeight(root: TreeNode?): Int {
43+
if (root == null) {
44+
return 0
45+
}
46+
var leftHeight = 0
47+
var rightHeight = 0
48+
// Left
49+
if (root.left != null) {
50+
leftHeight = getMaxHeight(root.left)
51+
}
52+
// Right
53+
if (root.right != null) {
54+
rightHeight = getMaxHeight(root.right)
55+
}
56+
return if (leftHeight > rightHeight) {
57+
1 + leftHeight
58+
} else {
59+
1 + rightHeight
60+
}
61+
}
62+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
110\. Balanced Binary Tree
2+
3+
Easy
4+
5+
Given a binary tree, determine if it is height-balanced.
6+
7+
For this problem, a height-balanced binary tree is defined as:
8+
9+
> a binary tree in which the left and right subtrees of _every_ node differ in height by no more than 1.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg)
14+
15+
**Input:** root = [3,9,20,null,null,15,7]
16+
17+
**Output:** true
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg)
22+
23+
**Input:** root = [1,2,2,3,3,null,null,4,4]
24+
25+
**Output:** false
26+
27+
**Example 3:**
28+
29+
**Input:** root = []
30+
31+
**Output:** true
32+
33+
**Constraints:**
34+
35+
* The number of nodes in the tree is in the range `[0, 5000]`.
36+
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>

0 commit comments

Comments
 (0)