Skip to content

Commit ea6fa27

Browse files
committed
feat: add solutions to lc problem: No.0230. Kth Smallest Element in a BST
1 parent 4cb071a commit ea6fa27

File tree

18 files changed

+584
-20
lines changed

18 files changed

+584
-20
lines changed

solution/0200-0299/0230.Kth Smallest Element in a BST/README.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,70 @@
5252
<!-- 这里可写当前语言的特殊实现逻辑 -->
5353

5454
```python
55-
55+
# Definition for a binary tree node.
56+
# class TreeNode:
57+
# def __init__(self, val=0, left=None, right=None):
58+
# self.val = val
59+
# self.left = left
60+
# self.right = right
61+
class Solution:
62+
def kthSmallest(self, root: TreeNode, k: int) -> int:
63+
def inorder(root):
64+
if root is None:
65+
return
66+
inorder(root.left)
67+
self.k -= 1
68+
if self.k == 0:
69+
self.res = root.val
70+
return
71+
inorder(root.right)
72+
self.k = k
73+
inorder(root)
74+
return self.res
5675
```
5776

5877
### **Java**
5978

6079
<!-- 这里可写当前语言的特殊实现逻辑 -->
6180

6281
```java
63-
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode() {}
89+
* TreeNode(int val) { this.val = val; }
90+
* TreeNode(int val, TreeNode left, TreeNode right) {
91+
* this.val = val;
92+
* this.left = left;
93+
* this.right = right;
94+
* }
95+
* }
96+
*/
97+
class Solution {
98+
private int k;
99+
private int res;
100+
101+
public int kthSmallest(TreeNode root, int k) {
102+
this.k = k;
103+
inorder(root);
104+
return res;
105+
}
106+
107+
private void inorder(TreeNode root) {
108+
if (root == null) {
109+
return;
110+
}
111+
inorder(root.left);
112+
if (--k == 0) {
113+
res = root.val;
114+
return;
115+
}
116+
inorder(root.right);
117+
}
118+
}
64119
```
65120

66121
### **...**

solution/0200-0299/0230.Kth Smallest Element in a BST/README_EN.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,68 @@
4040
### **Python3**
4141

4242
```python
43-
43+
# Definition for a binary tree node.
44+
# class TreeNode:
45+
# def __init__(self, val=0, left=None, right=None):
46+
# self.val = val
47+
# self.left = left
48+
# self.right = right
49+
class Solution:
50+
def kthSmallest(self, root: TreeNode, k: int) -> int:
51+
def inorder(root):
52+
if root is None:
53+
return
54+
inorder(root.left)
55+
self.k -= 1
56+
if self.k == 0:
57+
self.res = root.val
58+
return
59+
inorder(root.right)
60+
self.k = k
61+
inorder(root)
62+
return self.res
4463
```
4564

4665
### **Java**
4766

4867
```java
49-
68+
/**
69+
* Definition for a binary tree node.
70+
* public class TreeNode {
71+
* int val;
72+
* TreeNode left;
73+
* TreeNode right;
74+
* TreeNode() {}
75+
* TreeNode(int val) { this.val = val; }
76+
* TreeNode(int val, TreeNode left, TreeNode right) {
77+
* this.val = val;
78+
* this.left = left;
79+
* this.right = right;
80+
* }
81+
* }
82+
*/
83+
class Solution {
84+
private int k;
85+
private int res;
86+
87+
public int kthSmallest(TreeNode root, int k) {
88+
this.k = k;
89+
inorder(root);
90+
return res;
91+
}
92+
93+
private void inorder(TreeNode root) {
94+
if (root == null) {
95+
return;
96+
}
97+
inorder(root.left);
98+
if (--k == 0) {
99+
res = root.val;
100+
return;
101+
}
102+
inorder(root.right);
103+
}
104+
}
50105
```
51106

52107
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
17+
private int k;
18+
private int res;
19+
220
public int kthSmallest(TreeNode root, int k) {
3-
Deque<TreeNode> stack = new ArrayDeque<>();
4-
while (!stack.isEmpty() || root != null) {
5-
while (root != null) {
6-
stack.push(root);
7-
root = root.left;
8-
}
9-
root = stack.pop();
10-
if (--k == 0) {
11-
return root.val;
12-
}
13-
root = root.right;
21+
this.k = k;
22+
inorder(root);
23+
return res;
24+
}
25+
26+
private void inorder(TreeNode root) {
27+
if (root == null) {
28+
return;
29+
}
30+
inorder(root.left);
31+
if (--k == 0) {
32+
res = root.val;
33+
return;
1434
}
15-
return 0;
35+
inorder(root.right);
1636
}
17-
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def kthSmallest(self, root: TreeNode, k: int) -> int:
9+
def inorder(root):
10+
if root is None:
11+
return
12+
inorder(root.left)
13+
self.k -= 1
14+
if self.k == 0:
15+
self.res = root.val
16+
return
17+
inorder(root.right)
18+
self.k = k
19+
inorder(root)
20+
return self.res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [1901. Find a Peak Element II](https://leetcode-cn.com/problems/find-a-peak-element-ii)
2+
3+
[English Version](/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>A <strong>peak</strong> element in a 2D grid is an element that is <strong>strictly greater</strong> than all of its <strong>adjacent </strong>neighbors to the left, right, top, and bottom.</p>
10+
11+
<p>Given a <strong>0-indexed</strong> <code>m x n</code> matrix <code>mat</code> where <strong>no two adjacent cells are equal</strong>, find <strong>any</strong> peak element <code>mat[i][j]</code> and return <em>the length 2 array </em><code>[i,j]</code>.</p>
12+
13+
<p>You may assume that the entire matrix is surrounded by an <strong>outer perimeter</strong> with the value <code>-1</code> in each cell.</p>
14+
15+
<p>You must write an algorithm that runs in <code>O(m log(n))</code> or <code>O(n log(m))</code> time.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong>Example 1:</strong></p>
19+
20+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/images/1.png" style="width: 206px; height: 209px;" /></p>
21+
22+
<pre>
23+
<strong>Input:</strong> mat = [[1,4],[3,2]]
24+
<strong>Output:</strong> [0,1]
25+
<strong>Explanation:</strong>&nbsp;Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.
26+
</pre>
27+
28+
<p><strong>Example 2:</strong></p>
29+
30+
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/images/3.png" style="width: 254px; height: 257px;" /></strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> mat = [[10,20,15],[21,30,14],[7,16,32]]
34+
<strong>Output:</strong> [1,1]
35+
<strong>Explanation:</strong>&nbsp;Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>m == mat.length</code></li>
43+
<li><code>n == mat[i].length</code></li>
44+
<li><code>1 &lt;= m, n &lt;= 500</code></li>
45+
<li><code>1 &lt;= mat[i][j] &lt;= 10<sup>5</sup></code></li>
46+
<li>No two adjacent cells are equal.</li>
47+
</ul>
48+
49+
50+
## 解法
51+
52+
<!-- 这里可写通用的实现逻辑 -->
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
60+
```python
61+
62+
```
63+
64+
### **Java**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```java
69+
70+
```
71+
72+
### **...**
73+
74+
```
75+
76+
```
77+
78+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [1901. Find a Peak Element II](https://leetcode.com/problems/find-a-peak-element-ii)
2+
3+
[中文文档](/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/README.md)
4+
5+
## Description
6+
7+
<p>A <strong>peak</strong> element in a 2D grid is an element that is <strong>strictly greater</strong> than all of its <strong>adjacent </strong>neighbors to the left, right, top, and bottom.</p>
8+
9+
<p>Given a <strong>0-indexed</strong> <code>m x n</code> matrix <code>mat</code> where <strong>no two adjacent cells are equal</strong>, find <strong>any</strong> peak element <code>mat[i][j]</code> and return <em>the length 2 array </em><code>[i,j]</code>.</p>
10+
11+
<p>You may assume that the entire matrix is surrounded by an <strong>outer perimeter</strong> with the value <code>-1</code> in each cell.</p>
12+
13+
<p>You must write an algorithm that runs in <code>O(m log(n))</code> or <code>O(n log(m))</code> time.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/images/1.png" style="width: 206px; height: 209px;" /></p>
19+
20+
<pre>
21+
<strong>Input:</strong> mat = [[1,4],[3,2]]
22+
<strong>Output:</strong> [0,1]
23+
<strong>Explanation:</strong>&nbsp;Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.
24+
</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1901.Find%20a%20Peak%20Element%20II/images/3.png" style="width: 254px; height: 257px;" /></strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> mat = [[10,20,15],[21,30,14],[7,16,32]]
32+
<strong>Output:</strong> [1,1]
33+
<strong>Explanation:</strong>&nbsp;Both 30 and 32 are peak elements so [1,1] and [2,2] are both acceptable answers.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>m == mat.length</code></li>
41+
<li><code>n == mat[i].length</code></li>
42+
<li><code>1 &lt;= m, n &lt;= 500</code></li>
43+
<li><code>1 &lt;= mat[i][j] &lt;= 10<sup>5</sup></code></li>
44+
<li>No two adjacent cells are equal.</li>
45+
</ul>
46+
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
62+
```
63+
64+
### **...**
65+
66+
```
67+
68+
```
69+
70+
<!-- tabs:end -->
Loading
Loading

0 commit comments

Comments
 (0)