algo/rust/24_binary_tree/search_in_binary_tree.rs
Caitlin Gao 89d6013c88 feat(geektime_algo): add 24 binary tree
insert in binary tree, search in binary tree, max depth in binary tree
2019-07-25 21:56:56 +08:00

26 lines
872 B
Rust

// https://leetcode.com/problems/search-in-a-binary-search-tree/
use super::util::tree::{TreeNode, to_tree};
// Iterating method
pub fn search_bst(root: Option<Rc<RefCell<TreeNode>>>, val: i32) -> Option<Rc<RefCell<TreeNode>>> {
let mut r = root.clone();
while let Some(node) = r {
if node.borrow().val == val { return Some(node); }
if node.borrow().val > val {
r = node.borrow().left.clone();
} else {
r = node.borrow().right.clone();
}
}
None
}
// Recursive Approach
pub fn search_bst(root: Option<Rc<RefCell<TreeNode>>>, val: i32) -> Option<Rc<RefCell<TreeNode>>> {
if let Some(n) = &root {
if n.borrow().val > val { return Self::search_bst(n.borrow().left.clone(), val); }
if n.borrow().val < val { return Self::search_bst(n.borrow().right.clone(), val); }
}
root
}