2476. Closest Nodes Queries in a Binary Search Tree

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traverse(TreeNode *root, vector<int> &ret){
if(root == nullptr) return;
traverse(root->left, ret);
ret.push_back(root->val);
traverse(root->right, ret);

}
vector<vector<int>> closestNodes(TreeNode* root, vector<int>& queries) {

vector<vector<int>> res(queries.size(), vector<int> (2, 0));
vector<int> nums;
traverse(root, nums);
int n = nums.size();
// for(int r:nums) cout<<r<<" ";
for(int q=0;q<queries.size() ; ++q){
if(queries[q]<nums[0]){
res[q][0] =-1;
res[q][1] = nums[0];
}
else if(queries[q] > nums[n-1]){
res[q][0] = nums[n-1];
res[q][1] = -1;
}
else{
// 1 2 4 6 9 13 14 15
// binary search
int l = 0, r = n-1;
while(l<=r){
int mid = l + (r-l)/2;
if(nums[mid] == queries[q]){
res[q][0] = nums[mid];
res[q][1] = nums[mid];
break;
}
if(nums[mid] > queries[q]) r = mid-1;
else l = mid+1;
}
if(res[q][0]==0)
{
res[q][0] = min( nums[l], nums[r]);
res[q][1] = max( nums[l], nums[r]);
}


}
}
return res;

}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traverse(TreeNode *root, vector<int> &ret){
if(root == nullptr) return;
traverse(root->left, ret);
ret.push_back(root->val);
traverse(root->right, ret);
}
vector<vector<int>> closestNodes(TreeNode* root, vector<int>& queries) {

vector<vector<int>> res;
vector<int> nums;
traverse(root, nums);
int n = nums.size();

for(int q: queries){
auto it = lower_bound(begin(nums), end(nums), q);
if (it != end(nums) && *it == q)
res.push_back({q, q});
else
res.push_back({it == begin(nums) ? -1 : *prev(it), it == end(nums) ? -1 : *it});


}
return res;

}
};

analysis

  • time complexity O(mlogn)
  • space complexity O(n)