0
0
DSA Goprogramming~10 mins

Floor and Ceil in BST in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start searching for the floor value in the BST.

DSA Go
func floorInBST(root *Node, key int) *Node {
    var floorNode *Node = nil
    current := [1]
    for current != nil {
        if current.val == key {
            return current
        } else if current.val > key {
            current = current.left
        } else {
            floorNode = current
            current = current.right
        }
    }
    return floorNode
}
Drag options to blanks, or click blank then click option'
Aroot.left
Bnil
Croot
Droot.right
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing current to nil causes no traversal.
Starting from root.left or root.right skips part of the tree.
2fill in blank
medium

Complete the code to update the ceilNode when current node value is greater than the key.

DSA Go
func ceilInBST(root *Node, key int) *Node {
    var ceilNode *Node = nil
    current := root
    for current != nil {
        if current.val == key {
            return current
        } else if current.val > key {
            ceilNode = [1]
            current = current.left
        } else {
            current = current.right
        }
    }
    return ceilNode
}
Drag options to blanks, or click blank then click option'
Acurrent
Bcurrent.right
Ccurrent.left
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning ceilNode to current.left or current.right instead of current.
Not updating ceilNode at all.
3fill in blank
hard

Fix the error in the recursive floor function to correctly return the floor node.

DSA Go
func floorRecursive(root *Node, key int) *Node {
    if root == nil {
        return nil
    }
    if root.val == key {
        return root
    }
    if root.val > key {
        return [1]
    }
    floorRight := floorRecursive(root.right, key)
    if floorRight != nil && floorRight.val <= key {
        return floorRight
    }
    return root
}
Drag options to blanks, or click blank then click option'
AfloorRecursive(root.left, key)
BfloorRecursive(root.right, key)
Croot.left
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Recursing on right subtree when root.val > key.
Returning nil too early.
4fill in blank
hard

Fill both blanks to complete the iterative ceil function in BST.

DSA Go
func ceilInBSTIter(root *Node, key int) *Node {
    var ceilNode *Node = nil
    current := root
    for current != nil {
        if current.val == key {
            return current
        } else if current.val < key {
            current = [1]
        } else {
            ceilNode = current
            current = [2]
        }
    }
    return ceilNode
}
Drag options to blanks, or click blank then click option'
Acurrent.right
Bcurrent.left
Croot
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right moves.
Not updating ceilNode before moving left.
5fill in blank
hard

Fill all three blanks to complete the recursive ceil function in BST.

DSA Go
func ceilRecursive(root *Node, key int) *Node {
    if root == nil {
        return nil
    }
    if root.val == key {
        return root
    }
    if root.val < key {
        return [1]
    }
    ceilLeft := [2]
    if ceilLeft != nil && ceilLeft.val >= key {
        return ceilLeft
    }
    return [3]
}
Drag options to blanks, or click blank then click option'
AceilRecursive(root.left, key)
BceilRecursive(root.right, key)
Croot.left
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Searching wrong subtree based on root.val and key.
Returning incorrect node when no ceil found in left subtree.