提交 c9eee8b8 编写于 作者: 梦境迷离's avatar 梦境迷离

rust leetcode

上级 363e4be9
...@@ -835,4 +835,30 @@ impl Solution { ...@@ -835,4 +835,30 @@ impl Solution {
c c
} }
} }
```
* 二叉搜索树的第k大节点
```rust
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn kth_largest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
let mut ret = Vec::new();
let mut nodes = VecDeque::new();
let mut cur = root.clone();
while cur.is_some() || !nodes.is_empty() {
while let Some(c) = cur {
nodes.push_back(Some(c.clone()));
cur = c.try_borrow().unwrap().left.clone();
}
if let Some(n) = nodes.pop_back().unwrap() {
ret.push(n.try_borrow().unwrap().val);
cur = n.try_borrow().unwrap().right.clone();
}
}
for e in ret.iter() {
println!("{}", e);
};
ret[(ret.len() - k as usize)]
}
}
``` ```
\ No newline at end of file
...@@ -1137,6 +1137,39 @@ fn leetcode_1385() { ...@@ -1137,6 +1137,39 @@ fn leetcode_1385() {
println!("{}", ret); println!("{}", ret);
} }
///二叉搜索树的第k大节点
fn interview_54() {
println!("interview_54");
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn kth_largest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
let mut ret = Vec::new();
let mut nodes = VecDeque::new();
let mut cur = root.clone();
while cur.is_some() || !nodes.is_empty() {
while let Some(c) = cur {
nodes.push_back(Some(c.clone()));
cur = c.try_borrow().unwrap().left.clone();
}
if let Some(n) = nodes.pop_back().unwrap() {
ret.push(n.try_borrow().unwrap().val);
cur = n.try_borrow().unwrap().right.clone();
}
}
for e in ret.iter() {
println!("{}", e);
};
ret[(ret.len() - k as usize)]
}
}
let e2 = Some(Rc::new(RefCell::new(TreeNode { val: 2, left: None, right: None })));
let e1 = Some(Rc::new(RefCell::new(TreeNode { val: 1, left: None, right: e2 })));
let e4 = Some(Rc::new(RefCell::new(TreeNode { val: 4, left: None, right: None })));
let root = Some(Rc::new(RefCell::new(TreeNode { val: 3, left: e1, right: e4 })));
println!("{:?}", Solution::kth_largest(root, 4))
}
///所有方法调用 ///所有方法调用
pub fn solutions() { pub fn solutions() {
interview_58_2(); interview_58_2();
...@@ -1172,6 +1205,7 @@ pub fn solutions() { ...@@ -1172,6 +1205,7 @@ pub fn solutions() {
leetcode_728(); leetcode_728();
interview_01_01(); interview_01_01();
leetcode_1385(); leetcode_1385();
interview_54();
} }
fn print_vec(nums: Vec<i32>) { fn print_vec(nums: Vec<i32>) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册