Skip to content

Commit cea509a

Browse files
committedFeb 15, 2022
feat: add solutions to lcof problem: No.54
面试题54. 二叉搜索树的第k大节点
1 parent 2e834a7 commit cea509a

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
 

‎lcof/面试题54. 二叉搜索树的第k大节点/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,82 @@ func inorder(ctx context.Context, cur *TreeNode, ch chan<- int) {
219219
}
220220
```
221221

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

224300
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
22+
impl Solution {
23+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, arr: &mut Vec<i32>) {
24+
if let Some(node) = root {
25+
let node = node.borrow();
26+
Solution::dfs(&node.right, arr);
27+
arr.push(node.val);
28+
Solution::dfs(&node.left, arr)
29+
}
30+
}
31+
pub fn kth_largest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
32+
let mut arr = vec![];
33+
Solution::dfs(&root, &mut arr);
34+
arr[(k - 1) as usize]
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 kthLargest(root: TreeNode | null, k: number): number {
16+
let res = 0;
17+
const dfs = (root: TreeNode | null) => {
18+
if (root == null) {
19+
return;
20+
}
21+
dfs(root.right);
22+
if (--k === 0) {
23+
res = root.val;
24+
return;
25+
}
26+
dfs(root.left);
27+
};
28+
dfs(root);
29+
return res;
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.