Prepin
Log in

Floor and Ceiling in a Binary Search Tree

Approach / Explanation

The inquiry pertains to the definitions of 'floor' and 'ceil' in relation to a Binary Search Tree (BST). In mathematical terms, the 'floor' of a number is the largest integer that is less than or equal to that number, while the 'ceil' is the smallest integer that is greater than or equal to that number. Within a Binary Search Tree, the 'floor' of a specific node is the node with the greatest value that is less than or equal to the value of that node, whereas the 'ceil' of that node is the node with the least value that is greater than or equal to the value of that node. To effectively address this question, candidates should have a solid understanding of the structure and characteristics of a BST, the principles of tree traversal, and the methods for performing comparison-based searches within such structures. Essential points to cover in the explanation could include: A description of Binary Search Trees. An explanation of the definitions and significance of 'floor' and 'ceil' in the context of BSTs. Illustrations of how to determine the 'floor' and 'ceil' of nodes in a BST.

Suggested Answer

In a Binary Search Tree, the 'floor' and 'ceil' of a specific node refer to the nodes that have the closest lower and higher values, respectively. To determine the 'floor' of a given node, we begin at the BST's root and perform a search. If the value of the root matches that of the target node, then the root is the 'floor'. If the root's value exceeds that of the target, we proceed to the left child. If the root’s value is less than the target, then the root might be the 'floor', but we need to explore the right sub-tree by moving to the right child. Finding the 'ceil' follows a similar, but reversed, procedure. We still start at the root; if the root's value matches the target, then the root is the 'ceil'. If the root’s value is lower than the target, we move to the right child. Conversely, if the root’s value is greater, the root could be the 'ceil', but it is necessary to check by moving to the left child.

Alternative Answer

The ideas of 'floor' and 'ceil' can be easily grasped through a Binary Search Tree. In a BST, the 'floor' of a node refers to the node with the largest value that is lower than the given key, while 'ceil' refers to the node with the smallest value that is higher than the key. To locate these values, we start traversing the BST from the root. To determine the 'floor' of a node, we use the following guidelines: If the value of the root matches the key, then the root is the floor(node). If the root's value is greater than the key, the floor must be in the left subtree, so we move left. If the root's value is less than the key, the root could be a candidate for the floor, and there may also be a floor in the right subtree. Similarly, to find the 'ceil' of a node, we follow these opposite guidelines: If the root's value equals the key, then the root is the ceil(node). If the root's value is less than the key, the ceil must be in the right subtree, so we move right. If the root's value is greater than the key, the root could be a candidate for the ceil, and there may also be a ceil in the left subtree.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.