700. Search in a Binary Search Tree Easy

@problem@discussion
#Tree#Binary Search Tree#Binary Tree



1/**
2 * [700] Search in a Binary Search Tree
3 *
4 * You are given the root of a binary search tree (BST) and an integer val.
5 * Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg" style="width: 422px; height: 302px;" />
9 * Input: root = [4,2,7,1,3], val = 2
10 * Output: [2,1,3]
11 * 
12 * Example 2:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg" style="width: 422px; height: 302px;" />
14 * Input: root = [4,2,7,1,3], val = 5
15 * Output: []
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	The number of nodes in the tree is in the range [1, 5000].
21 * 	1 <= Node.val <= 10^7
22 * 	root is a binary search tree.
23 * 	1 <= val <= 10^7
24 * 
25 */
26pub struct Solution {}
27use crate::util::tree::{TreeNode, to_tree};
28
29// problem: https://leetcode.com/problems/search-in-a-binary-search-tree/
30// discuss: https://leetcode.com/problems/search-in-a-binary-search-tree/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for a binary tree node.
35// #[derive(Debug, PartialEq, Eq)]
36// pub struct TreeNode {
37//   pub val: i32,
38//   pub left: Option<Rc<RefCell<TreeNode>>>,
39//   pub right: Option<Rc<RefCell<TreeNode>>>,
40// }
41// 
42// impl TreeNode {
43//   #[inline]
44//   pub fn new(val: i32) -> Self {
45//     TreeNode {
46//       val,
47//       left: None,
48//       right: None
49//     }
50//   }
51// }
52use std::rc::Rc;
53use std::cell::RefCell;
54impl Solution {
55    pub fn search_bst(root: Option<Rc<RefCell<TreeNode>>>, val: i32) -> Option<Rc<RefCell<TreeNode>>> {
56        Some(Rc::new(RefCell::new(TreeNode::new(0))))
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_700() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.