Floor in BST
Floor in BST is a easy trees & bst problem solved with the miscellaneous bst questions pattern.
The best approach, optimal (walk down the bst), runs in O(h) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from traverse everything up.
Problem
Given a BST and a value x, return the floor of x in the tree: the largest value that is less than or equal to x, or -1 if there is none.
Examples
Example 1
- Input
root = [10, 5, 15, 2, 6], x = 7- Output
6
Example 2
- Input
root = [10, 5, 15, 2, 6], x = 1- Output
-1
Constraints
- The tree has
1to10^5nodes. - Return -1 if no value is <= x.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Traverse everything | O(n) | O(h) |
| Optimal (walk down the BST) | O(h) | O(1) |
1Traverse everything
O(n)O(h)Visit every node and keep the largest value that is still <= x.
- DFS all nodes; track the best value <= x.
class Solution {
public static int floor(TreeNode root, int x) {
if (root == null) return -1;
int best = root.val <= x ? root.val : -1;
return Math.max(best, Math.max(floor(root.left, x), floor(root.right, x)));
}
}class Solution:
def floor(self, root: Optional[TreeNode], x: int) -> int:
if root is None:
return -1
best = root.val if root.val <= x else -1
return max(best, max(self.floor(root.left, x), self.floor(root.right, x)))class Solution {
public:
static int floor(TreeNode* root, int x) {
if (root == nullptr) return -1;
int best = root->val <= x ? root->val : -1;
return max(best, max(floor(root->left, x), floor(root->right, x)));
}
};function floor(root, x) {
if (root === null) return -1;
const best = root.val <= x ? root.val : -1;
return Math.max(best, Math.max(floor(root.left, x), floor(root.right, x)));
}2Optimal (walk down the BST)
O(h)O(1)At each node: equal returns it; greater than x means the floor is to the left; smaller means it is a candidate, and a better one may be to the right.
- ans = -1.
- val == x → return x. val > x → go left. Else ans = val, go right.
class Solution {
public static int floor(TreeNode root, int x) {
int ans = -1;
while (root != null) {
if (root.val == x) return x;
if (root.val > x) root = root.left;
else { ans = root.val; root = root.right; }
}
return ans;
}
}class Solution:
def floor(self, root: Optional[TreeNode], x: int) -> int:
ans = -1
while root is not None:
if root.val == x:
return x
if root.val > x:
root = root.left
else:
ans = root.val
root = root.right
return ansclass Solution {
public:
static int floor(TreeNode* root, int x) {
int ans = -1;
while (root != nullptr) {
if (root->val == x) return x;
if (root->val > x) root = root->left;
else { ans = root->val; root = root->right; }
}
return ans;
}
};function floor(root, x) {
let ans = -1;
while (root !== null) {
if (root.val === x) return x;
if (root.val > x) root = root.left;
else { ans = root.val; root = root.right; }
}
return ans;
}Edge cases to test
- x equals a node (the answer is that node)
- x below the minimum
Hints
Hint 1
Like predecessor, but equal counts: a node <= x is a candidate, so go right to look for a bigger one.
FAQ
What is the best time complexity for Floor in BST?
Optimal (walk down the BST) runs in O(h) time and O(1) extra space.
Which pattern does Floor in BST use?
It is a trees & bst problem that uses the miscellaneous bst questions pattern. Other problems with the same pattern: Inorder Successor in BST, Inorder predecessor, Ceil in BST.
Is there a brute force solution for Floor in BST?
Yes. Traverse everything takes O(n) time and O(h) space. Visit every node and keep the largest value that is still <= x.
Which edge cases should I test for Floor in BST?
x equals a node (the answer is that node); x below the minimum.