mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge pull request #1795 from fwqaaq/patch-7
Update 0501.二叉搜索树中的众数.md about rust
This commit is contained in:
@ -837,6 +837,93 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## rust
|
||||||
|
|
||||||
|
递归:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn find_mode(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||||
|
let mut count = 0;
|
||||||
|
let mut max_count = 0;
|
||||||
|
let mut res = vec![];
|
||||||
|
let mut pre = i32::MIN;
|
||||||
|
Self::search_bst(&root, &mut pre, &mut res, &mut count, &mut max_count);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
pub fn search_bst(
|
||||||
|
cur: &Option<Rc<RefCell<TreeNode>>>,
|
||||||
|
mut pre: &mut i32,
|
||||||
|
res: &mut Vec<i32>,
|
||||||
|
count: &mut i32,
|
||||||
|
max_count: &mut i32,
|
||||||
|
) {
|
||||||
|
if cur.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cur_node = cur.as_ref().unwrap().borrow();
|
||||||
|
Self::search_bst(&cur_node.left, pre, res, count, max_count);
|
||||||
|
if *pre == i32::MIN {
|
||||||
|
*count = 1;
|
||||||
|
} else if *pre == cur_node.val {
|
||||||
|
*count += 1;
|
||||||
|
} else {
|
||||||
|
*count = 1;
|
||||||
|
};
|
||||||
|
match count.cmp(&max_count) {
|
||||||
|
std::cmp::Ordering::Equal => res.push(cur_node.val),
|
||||||
|
std::cmp::Ordering::Greater => {
|
||||||
|
*max_count = *count;
|
||||||
|
res.clear();
|
||||||
|
res.push(cur_node.val);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
*pre = cur_node.val;
|
||||||
|
Self::search_bst(&cur_node.right, pre, res, count, max_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub fn find_mode(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||||
|
let (mut cur, mut pre) = (root, i32::MIN);
|
||||||
|
let mut res = vec![];
|
||||||
|
let mut stack = vec![];
|
||||||
|
let (mut count, mut max_count) = (0, 0);
|
||||||
|
while !stack.is_empty() || cur.is_some() {
|
||||||
|
while let Some(node) = cur {
|
||||||
|
cur = node.borrow().left.clone();
|
||||||
|
stack.push(node);
|
||||||
|
}
|
||||||
|
if let Some(node) = stack.pop() {
|
||||||
|
if pre == i32::MIN {
|
||||||
|
count = 1;
|
||||||
|
} else if pre == node.borrow().val {
|
||||||
|
count += 1;
|
||||||
|
} else {
|
||||||
|
count = 1;
|
||||||
|
}
|
||||||
|
match count.cmp(&max_count) {
|
||||||
|
std::cmp::Ordering::Equal => res.push(node.borrow().val),
|
||||||
|
std::cmp::Ordering::Greater => {
|
||||||
|
max_count = count;
|
||||||
|
res.clear();
|
||||||
|
res.push(node.borrow().val);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
pre = node.borrow().val;
|
||||||
|
cur = node.borrow().right.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user