Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the BST node is None.
DSA Python
if root [1] None: return False
Attempts:
3 left
š” Hint
Common Mistakes
Using '!=' instead of '==' causes wrong logic.
Using '<' or '>' with None causes errors.
ā Incorrect
We check if the root is equal to None to know if we reached a leaf's child.
2fill in blank
mediumComplete the code to add a value to the set of seen values.
DSA Python
seen.[1](root.val)Attempts:
3 left
š” Hint
Common Mistakes
Using remove() causes errors if element not present.
Using pop() removes an arbitrary element.
ā Incorrect
We use the add() method to insert a value into a set.
3fill in blank
hardFix the error in checking if the complement exists in the set.
DSA Python
if [1] in seen: return True
Attempts:
3 left
š” Hint
Common Mistakes
Using addition instead of subtraction.
Using root.val alone instead of complement.
ā Incorrect
The complement is target minus the current node's value.
4fill in blank
hardFill both blanks to recursively search left and right subtrees.
DSA Python
return dfs(root.[1], target, seen) or dfs(root.[2], target, seen)
Attempts:
3 left
š” Hint
Common Mistakes
Using 'parent' or 'child' which are not node attributes.
Searching only one subtree misses possible pairs.
ā Incorrect
We search both left and right children recursively.
5fill in blank
hardFill all three blanks to complete the Two Sum in BST function.
DSA Python
def findTarget(root, target): def dfs(node, target, seen): if node [1] None: return False if (target - node.val) [2] seen: return True seen.[3](node.val) return dfs(node.left, target, seen) or dfs(node.right, target, seen) return dfs(root, target, set())
Attempts:
3 left
š” Hint
Common Mistakes
Using '!=' instead of '==' for None check.
Using 'not in' instead of 'in' for complement check.
Using remove or pop instead of add for set insertion.
ā Incorrect
The function checks if node is None, then if complement is in seen, then adds current value to seen.
