Skip to content

Commit 4a1d2b8

Browse files
authored
Added tasks 589-592
1 parent 8640db6 commit 4a1d2b8

File tree

8 files changed

+412
-78
lines changed

8 files changed

+412
-78
lines changed

README.md

Lines changed: 81 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
33
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
44
5-
* [Programming Skills I](#programming-skills-i)
65
* [Programming Skills II](#programming-skills-ii)
76
* [Graph Theory I](#graph-theory-i)
87
* [SQL I](#sql-i)
@@ -16,80 +15,7 @@
1615
* [Binary Search I](#binary-search-i)
1716
* [Binary Search II](#binary-search-ii)
1817
* [Dynamic Programming I](#dynamic-programming-i)
19-
20-
### Programming Skills I
21-
22-
#### Day 1 Basic Data Type
23-
24-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
25-
|-|-|-|-|-|-
26-
27-
#### Day 2 Operator
28-
29-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
30-
|-|-|-|-|-|-
31-
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
32-
33-
#### Day 3 Conditional Statements
34-
35-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
36-
|-|-|-|-|-|-
37-
38-
#### Day 4 Loop
39-
40-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
41-
|-|-|-|-|-|-
42-
| 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08
43-
44-
#### Day 5 Function
45-
46-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
47-
|-|-|-|-|-|-
48-
| 0496 |[Next Greater Element I](src/main/kotlin/g0401_0500/s0496_next_greater_element_i)| Easy | Array, Hash_Table, Stack, Monotonic_Stack | 171 | 100.00
49-
50-
#### Day 6 Array
51-
52-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
53-
|-|-|-|-|-|-
54-
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07
55-
56-
#### Day 7 Array
57-
58-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
59-
|-|-|-|-|-|-
60-
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation | 239 | 99.05
61-
62-
#### Day 8 String
63-
64-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
65-
|-|-|-|-|-|-
66-
| 0389 |[Find the Difference](src/main/kotlin/g0301_0400/s0389_find_the_difference)| Easy | String, Hash_Table, Sorting, Bit_Manipulation | 256 | 64.81
67-
68-
#### Day 9 String
69-
70-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
71-
|-|-|-|-|-|-
72-
73-
#### Day 10 Linked List and Tree
74-
75-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
76-
|-|-|-|-|-|-
77-
| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
78-
| 0404 |[Sum of Left Leaves](src/main/kotlin/g0401_0500/s0404_sum_of_left_leaves)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 173 | 86.05
79-
80-
#### Day 11 Containers and Libraries
81-
82-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
83-
|-|-|-|-|-|-
84-
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks)| Easy | Stack, Design, Queue | 258 | 70.86
85-
| 0242 |[Valid Anagram](src/main/kotlin/g0201_0300/s0242_valid_anagram)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 251 | 87.65
86-
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
87-
88-
#### Day 12 Class and Object
89-
90-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
91-
|-|-|-|-|-|-
92-
| 0303 |[Range Sum Query - Immutable](src/main/kotlin/g0301_0400/s0303_range_sum_query_immutable)| Easy | Array, Design, Prefix_Sum | 472 | 63.64
18+
* [Programming Skills I](#programming-skills-i)
9319

9420
### Programming Skills II
9521

@@ -397,6 +323,7 @@
397323

398324
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
399325
|-|-|-|-|-|-
326+
| 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal)| Easy | Depth_First_Search, Tree, Stack | 233 | 84.02
400327
| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 332 | 67.53
401328

402329
#### Day 7 Binary Search
@@ -1620,6 +1547,81 @@
16201547
| 0343 |[Integer Break](src/main/kotlin/g0301_0400/s0343_integer_break)| Medium | Dynamic_Programming, Math | 218 | 63.89
16211548
| 0279 |[Perfect Squares](src/main/kotlin/g0201_0300/s0279_perfect_squares)| Medium | Top_Interview_Questions, Dynamic_Programming, Math, Breadth_First_Search | 176 | 98.80
16221549

1550+
### Programming Skills I
1551+
1552+
#### Day 1 Basic Data Type
1553+
1554+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1555+
|-|-|-|-|-|-
1556+
1557+
#### Day 2 Operator
1558+
1559+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1560+
|-|-|-|-|-|-
1561+
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
1562+
1563+
#### Day 3 Conditional Statements
1564+
1565+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1566+
|-|-|-|-|-|-
1567+
1568+
#### Day 4 Loop
1569+
1570+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1571+
|-|-|-|-|-|-
1572+
| 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08
1573+
1574+
#### Day 5 Function
1575+
1576+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1577+
|-|-|-|-|-|-
1578+
| 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal)| Easy | Depth_First_Search, Tree, Stack | 233 | 84.02
1579+
| 0496 |[Next Greater Element I](src/main/kotlin/g0401_0500/s0496_next_greater_element_i)| Easy | Array, Hash_Table, Stack, Monotonic_Stack | 171 | 100.00
1580+
1581+
#### Day 6 Array
1582+
1583+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1584+
|-|-|-|-|-|-
1585+
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07
1586+
1587+
#### Day 7 Array
1588+
1589+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1590+
|-|-|-|-|-|-
1591+
| 0566 |[Reshape the Matrix](src/main/kotlin/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation | 239 | 99.05
1592+
1593+
#### Day 8 String
1594+
1595+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1596+
|-|-|-|-|-|-
1597+
| 0389 |[Find the Difference](src/main/kotlin/g0301_0400/s0389_find_the_difference)| Easy | String, Hash_Table, Sorting, Bit_Manipulation | 256 | 64.81
1598+
1599+
#### Day 9 String
1600+
1601+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1602+
|-|-|-|-|-|-
1603+
1604+
#### Day 10 Linked List and Tree
1605+
1606+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1607+
|-|-|-|-|-|-
1608+
| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
1609+
| 0404 |[Sum of Left Leaves](src/main/kotlin/g0401_0500/s0404_sum_of_left_leaves)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 173 | 86.05
1610+
1611+
#### Day 11 Containers and Libraries
1612+
1613+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1614+
|-|-|-|-|-|-
1615+
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks)| Easy | Stack, Design, Queue | 258 | 70.86
1616+
| 0242 |[Valid Anagram](src/main/kotlin/g0201_0300/s0242_valid_anagram)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 251 | 87.65
1617+
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
1618+
1619+
#### Day 12 Class and Object
1620+
1621+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1622+
|-|-|-|-|-|-
1623+
| 0303 |[Range Sum Query - Immutable](src/main/kotlin/g0301_0400/s0303_range_sum_query_immutable)| Easy | Array, Design, Prefix_Sum | 472 | 63.64
1624+
16231625
## Algorithms
16241626

16251627
| # | Title | Difficulty | Tag | Time, ms | Time, %
@@ -1630,6 +1632,10 @@
16301632
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16311633
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16321634
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1635+
| 0592 |[Fraction Addition and Subtraction](src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction)| Medium | String, Math, Simulation | 164 | 100.00
1636+
| 0591 |[Tag Validator](src/main/kotlin/g0501_0600/s0591_tag_validator)| Hard | String, Stack | 177 | 100.00
1637+
| 0590 |[N-ary Tree Postorder Traversal](src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal)| Easy | Depth_First_Search, Tree, Stack | 237 | 88.10
1638+
| 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal)| Easy | Depth_First_Search, Tree, Stack, Programming_Skills_I_Day_5_Function, Level_1_Day_6_Tree | 233 | 84.02
16331639
| 0587 |[Erect the Fence](src/main/kotlin/g0501_0600/s0587_erect_the_fence)| Hard | Array, Math, Geometry | 470 | 100.00
16341640
| 0586 |[Customer Placing the Largest Number of Orders](src/main/kotlin/g0501_0600/s0586_customer_placing_the_largest_number_of_orders)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_8_Function | 768 | 44.85
16351641
| 0584 |[Find Customer Referee](src/main/kotlin/g0501_0600/s0584_find_customer_referee)| Easy | Database, SQL_I_Day_1_Select | 779 | 43.48

src/main/kotlin/g0501_0600/s0530_minimum_absolute_difference_in_bst/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Given the `root` of a Binary Search Tree (BST), return _the minimum absolute dif
3535
```kotlin
3636
import com_github_leetcode.TreeNode
3737

38-
3938
/*
4039
* Example:
4140
* var ti = TreeNode(5)

src/main/kotlin/g0501_0600/s0572_subtree_of_another_tree/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ A subtree of a binary tree `tree` is a tree that consists of a node in `tree` an
3737
```kotlin
3838
import com_github_leetcode.TreeNode
3939

40-
4140
/*
4241
* Example:
4342
* var ti = TreeNode(5)

src/main/kotlin/g0501_0600/s0587_erect_the_fence/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import kotlin.math.atan2
4646
import kotlin.math.pow
4747
import kotlin.math.sqrt
4848

49-
5049
class Solution {
5150
private fun dist(p1: Pair<Int, Int>, p2: Pair<Int, Int>): Double {
5251
return sqrt((p2.second - p1.second).toDouble().pow(2.0) + Math.pow((p2.first - p1.first).toDouble(), 2.0))
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 589\. N-ary Tree Preorder Traversal
5+
6+
Easy
7+
8+
Given the `root` of an n-ary tree, return _the preorder traversal of its nodes' values_.
9+
10+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
15+
16+
**Input:** root = [1,null,3,2,4,null,5,6]
17+
18+
**Output:** [1,3,5,6,2,4]
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
23+
24+
**Input:** root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
25+
26+
**Output:** [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
27+
28+
**Constraints:**
29+
30+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
31+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
32+
* The height of the n-ary tree is less than or equal to `1000`.
33+
34+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
35+
36+
## Solution
37+
38+
```kotlin
39+
import com_github_leetcode.Node
40+
41+
/*
42+
* Definition for a Node.
43+
* class Node(var `val`: Int) {
44+
* var children: List<Node?> = listOf()
45+
* }
46+
*/
47+
class Solution {
48+
fun preorder(root: Node?): List<Int> {
49+
val res: MutableList<Int> = ArrayList()
50+
preorderHelper(res, root)
51+
return res
52+
}
53+
54+
private fun preorderHelper(res: MutableList<Int>, root: Node?) {
55+
if (root == null) {
56+
return
57+
}
58+
res.add(root.`val`)
59+
for (node in root.neighbors) {
60+
preorderHelper(res, node)
61+
}
62+
}
63+
}
64+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 590\. N-ary Tree Postorder Traversal
5+
6+
Easy
7+
8+
Given the `root` of an n-ary tree, return _the postorder traversal of its nodes' values_.
9+
10+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
15+
16+
**Input:** root = [1,null,3,2,4,null,5,6]
17+
18+
**Output:** [5,6,3,2,4,1]
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
23+
24+
**Input:** root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
25+
26+
**Output:** [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
27+
28+
**Constraints:**
29+
30+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
31+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
32+
* The height of the n-ary tree is less than or equal to `1000`.
33+
34+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
35+
36+
## Solution
37+
38+
```kotlin
39+
import com_github_leetcode.Node
40+
41+
/*
42+
* Definition for a Node.
43+
* class Node(var `val`: Int) {
44+
* var children: List<Node?> = listOf()
45+
* }
46+
*/
47+
class Solution {
48+
private var ans: ArrayList<Int>? = null
49+
50+
fun postorder(root: Node?): List<Int> {
51+
ans = ArrayList()
52+
recursion(root)
53+
if (root != null) {
54+
ans!!.add(root.`val`)
55+
}
56+
return ans as ArrayList<Int>
57+
}
58+
59+
private fun recursion(root: Node?) {
60+
if (root == null) {
61+
return
62+
}
63+
for (child in root.neighbors) {
64+
recursion(child)
65+
ans!!.add(child.`val`)
66+
}
67+
}
68+
}
69+
```

0 commit comments

Comments
 (0)