class Node: def __init__(self, val): self.val = val self.left = None self.right = None def bst_search(root, target): if root is None: return False if root.val == target: return True elif target < root.val: return bst_search(root.left, target) else: return bst_search(root.right, target) # Construct BST root = Node(10) root.left = Node(5) root.right = Node(20) root.left.left = Node(3) root.left.right = Node(7) root.right.left = Node(15) root.right.right = Node(30) result = bst_search(root, 15) print(result)
The value 15 exists in the BST as the left child of 20. The search function returns True when it finds the target.
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def bst_search(root, target): if root is None: return False if root.val == target: return True elif target < root.val: return bst_search(root.left, target) else: return bst_search(root.right, target) # Construct BST root = Node(10) root.left = Node(5) root.right = Node(20) root.left.left = Node(3) root.left.right = Node(7) root.right.left = Node(15) root.right.right = Node(30) result = bst_search(root, 25) print(result)
The value 25 does not exist in the BST. The search function returns False when the target is not found.
Starting at 10, since 7 < 10, go left to 5. Then 7 > 5, go right to 7. So nodes visited are 10, 5, 7.
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def bst_search(root, target): if root is None: return False if root.val == target: return True elif target > root.val: return bst_search(root.left, target) else: return bst_search(root.right, target) # Construct BST root = Node(10) root.left = Node(5) root.right = Node(20) root.left.left = Node(3) root.left.right = Node(7) root.right.left = Node(15) root.right.right = Node(30) result = bst_search(root, 15) print(result)
The code incorrectly searches left subtree when target > root.val, so it misses the correct node and returns False.
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def bst_search(root, target): if root is None: return False if root.val == target: return True elif target < root.val: return bst_search(root.left, target) else: return bst_search(root.right, target) # Construct BST root = Node(8) root.left = Node(4) root.right = Node(12) root.left.left = Node(2) root.left.right = Node(6) root.left.left.left = Node(1) root.left.left.right = Node(3) # Count recursive calls call_count = 0 def bst_search_count(root, target): global call_count call_count += 1 if root is None: return False if root.val == target: return True elif target < root.val: return bst_search_count(root.left, target) else: return bst_search_count(root.right, target) bst_search_count(root, 3) print(call_count)
The search visits nodes 8, 4, 2, then 3. That's 4 nodes, so 4 calls. But the initial call counts as 1, so total calls are 4.
Wait, initial call is the first call, so total calls = 4.
Recount: calls for nodes 8, 4, 2, 3 = 4 calls total.
Therefore, correct answer is 4.
