mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0700.二叉搜索树中的搜索.md
This commit is contained in:
@ -441,6 +441,29 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
迭代:
|
||||
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::cmp;
|
||||
impl Solution {
|
||||
pub fn search_bst(
|
||||
mut root: Option<Rc<RefCell<TreeNode>>>,
|
||||
val: i32,
|
||||
) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
while let Some(ref node) = root.clone() {
|
||||
match val.cmp(&node.borrow().val) {
|
||||
cmp::Ordering::Less => root = node.borrow().left.clone(),
|
||||
cmp::Ordering::Equal => return root,
|
||||
cmp::Ordering::Greater => root = node.borrow().right.clone(),
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user