Skip to content

Commit cd709ab

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

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,87 @@ func kthSmallest(root *TreeNode, k int) int {
188188
}
189189
```
190190

191+
### **TypeScript**
192+
193+
```ts
194+
/**
195+
* Definition for a binary tree node.
196+
* class TreeNode {
197+
* val: number
198+
* left: TreeNode | null
199+
* right: TreeNode | null
200+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
201+
* this.val = (val===undefined ? 0 : val)
202+
* this.left = (left===undefined ? null : left)
203+
* this.right = (right===undefined ? null : right)
204+
* }
205+
* }
206+
*/
207+
208+
function kthSmallest(root: TreeNode | null, k: number): number {
209+
const dfs = (root: TreeNode | null) => {
210+
if (root == null) {
211+
return -1;
212+
}
213+
const { val, left, right } = root;
214+
const l = dfs(left);
215+
if (l !== -1) {
216+
return l;
217+
}
218+
k--;
219+
if (k === 0) {
220+
return val;
221+
}
222+
return dfs(right);
223+
};
224+
return dfs(root);
225+
}
226+
```
227+
228+
### **Rust**
229+
230+
```rust
231+
// Definition for a binary tree node.
232+
// #[derive(Debug, PartialEq, Eq)]
233+
// pub struct TreeNode {
234+
// pub val: i32,
235+
// pub left: Option<Rc<RefCell<TreeNode>>>,
236+
// pub right: Option<Rc<RefCell<TreeNode>>>,
237+
// }
238+
//
239+
// impl TreeNode {
240+
// #[inline]
241+
// pub fn new(val: i32) -> Self {
242+
// TreeNode {
243+
// val,
244+
// left: None,
245+
// right: None
246+
// }
247+
// }
248+
// }
249+
use std::rc::Rc;
250+
use std::cell::RefCell;
251+
impl Solution {
252+
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>, k: usize) {
253+
if let Some(node) = root {
254+
let mut node = node.borrow_mut();
255+
Self::dfs(node.left.take(), res, k);
256+
res.push(node.val);
257+
if res.len() >= k {
258+
return;
259+
}
260+
Self::dfs(node.right.take(), res, k);
261+
}
262+
}
263+
pub fn kth_smallest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
264+
let k = k as usize;
265+
let mut res: Vec<i32> = Vec::with_capacity(k);
266+
Self::dfs(root, &mut res, k);
267+
res[k - 1]
268+
}
269+
}
270+
```
271+
191272
### **...**
192273

193274
```

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,87 @@ func kthSmallest(root *TreeNode, k int) int {
226226
}
227227
```
228228

229+
### **TypeScript**
230+
231+
```ts
232+
/**
233+
* Definition for a binary tree node.
234+
* class TreeNode {
235+
* val: number
236+
* left: TreeNode | null
237+
* right: TreeNode | null
238+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
239+
* this.val = (val===undefined ? 0 : val)
240+
* this.left = (left===undefined ? null : left)
241+
* this.right = (right===undefined ? null : right)
242+
* }
243+
* }
244+
*/
245+
246+
function kthSmallest(root: TreeNode | null, k: number): number {
247+
const dfs = (root: TreeNode | null) => {
248+
if (root == null) {
249+
return -1;
250+
}
251+
const { val, left, right } = root;
252+
const l = dfs(left);
253+
if (l !== -1) {
254+
return l;
255+
}
256+
k--;
257+
if (k === 0) {
258+
return val;
259+
}
260+
return dfs(right);
261+
};
262+
return dfs(root);
263+
}
264+
```
265+
266+
### **Rust**
267+
268+
```rust
269+
// Definition for a binary tree node.
270+
// #[derive(Debug, PartialEq, Eq)]
271+
// pub struct TreeNode {
272+
// pub val: i32,
273+
// pub left: Option<Rc<RefCell<TreeNode>>>,
274+
// pub right: Option<Rc<RefCell<TreeNode>>>,
275+
// }
276+
//
277+
// impl TreeNode {
278+
// #[inline]
279+
// pub fn new(val: i32) -> Self {
280+
// TreeNode {
281+
// val,
282+
// left: None,
283+
// right: None
284+
// }
285+
// }
286+
// }
287+
use std::rc::Rc;
288+
use std::cell::RefCell;
289+
impl Solution {
290+
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>, k: usize) {
291+
if let Some(node) = root {
292+
let mut node = node.borrow_mut();
293+
Self::dfs(node.left.take(), res, k);
294+
res.push(node.val);
295+
if res.len() >= k {
296+
return;
297+
}
298+
Self::dfs(node.right.take(), res, k);
299+
}
300+
}
301+
pub fn kth_smallest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
302+
let k = k as usize;
303+
let mut res: Vec<i32> = Vec::with_capacity(k);
304+
Self::dfs(root, &mut res, k);
305+
res[k - 1]
306+
}
307+
}
308+
```
309+
229310
### **...**
230311

231312
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>, k: usize) {
23+
if let Some(node) = root {
24+
let mut node = node.borrow_mut();
25+
Self::dfs(node.left.take(), res, k);
26+
res.push(node.val);
27+
if res.len() >= k {
28+
return;
29+
}
30+
Self::dfs(node.right.take(), res, k);
31+
}
32+
}
33+
pub fn kth_smallest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
34+
let k = k as usize;
35+
let mut res: Vec<i32> = Vec::with_capacity(k);
36+
Self::dfs(root, &mut res, k);
37+
res[k - 1]
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function kthSmallest(root: TreeNode | null, k: number): number {
16+
const dfs = (root: TreeNode | null) => {
17+
if (root == null) {
18+
return -1;
19+
}
20+
const { val, left, right } = root;
21+
const l = dfs(left);
22+
if (l !== -1) {
23+
return l;
24+
}
25+
k--;
26+
if (k === 0) {
27+
return val;
28+
}
29+
return dfs(right);
30+
};
31+
return dfs(root);
32+
}

0 commit comments

Comments
 (0)