2476. Closest Nodes Queries in a Binary Search Tree Medium
1/**
2 * [2476] Closest Nodes Queries in a Binary Search Tree
3 *
4 * You are given the root of a binary search tree and an array queries of size n consisting of positive integers.
5 * Find a 2D array answer of size n where answer[i] = [mini, maxi]:
6 *
7 * mini is the largest value in the tree that is smaller than or equal to queries[i]. If a such value does not exist, add -1 instead.
8 * maxi is the smallest value in the tree that is greater than or equal to queries[i]. If a such value does not exist, add -1 instead.
9 *
10 * Return the array answer.
11 *
12 * <strong class="example">Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2022/09/28/bstreeedrawioo.png" style="width: 261px; height: 281px;" />
14 * Input: root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16]
15 * Output: [[2,2],[4,6],[15,-1]]
16 * Explanation: We answer the queries in the following way:
17 * - The largest number that is smaller or equal than 2 in the tree is 2, and the smallest number that is greater or equal than 2 is still 2. So the answer for the first query is [2,2].
18 * - The largest number that is smaller or equal than 5 in the tree is 4, and the smallest number that is greater or equal than 5 is 6. So the answer for the second query is [4,6].
19 * - The largest number that is smaller or equal than 16 in the tree is 15, and the smallest number that is greater or equal than 16 does not exist. So the answer for the third query is [15,-1].
20 *
21 * <strong class="example">Example 2:
22 * <img alt="" src="https://assets.leetcode.com/uploads/2022/09/28/bstttreee.png" style="width: 101px; height: 121px;" />
23 * Input: root = [4,null,9], queries = [3]
24 * Output: [[-1,4]]
25 * Explanation: The largest number that is smaller or equal to 3 in the tree does not exist, and the smallest number that is greater or equal to 3 is 4. So the answer for the query is [-1,4].
26 *
27 *
28 * Constraints:
29 *
30 * The number of nodes in the tree is in the range [2, 10^5].
31 * 1 <= Node.val <= 10^6
32 * n == queries.length
33 * 1 <= n <= 10^5
34 * 1 <= queries[i] <= 10^6
35 *
36 */
37pub struct Solution {}
38use crate::util::tree::{TreeNode, to_tree};
39
40// problem: https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/
41// discuss: https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45// Definition for a binary tree node.
46// #[derive(Debug, PartialEq, Eq)]
47// pub struct TreeNode {
48// pub val: i32,
49// pub left: Option<Rc<RefCell<TreeNode>>>,
50// pub right: Option<Rc<RefCell<TreeNode>>>,
51// }
52//
53// impl TreeNode {
54// #[inline]
55// pub fn new(val: i32) -> Self {
56// TreeNode {
57// val,
58// left: None,
59// right: None
60// }
61// }
62// }
63use std::rc::Rc;
64use std::cell::RefCell;
65impl Solution {
66 pub fn closest_nodes(root: Option<Rc<RefCell<TreeNode>>>, queries: Vec<i32>) -> Vec<Vec<i32>> {
67 vec![]
68 }
69}
70
71// submission codes end
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_2476() {
79 }
80}
81
Back
© 2025 bowen.ge All Rights Reserved.