Complete the code to insert a new node in a Binary Search Tree (BST).
if root is None: root = [1](key)
The insertion starts by creating a new node if the current root is None. The class used to create a node is typically named Node.
Complete the code to decide where to insert the new key in the BST.
if key < root.key: root.left = insert(root.left, [1])
When inserting in a BST, if the new key is less than the current node's key, we recursively insert it into the left subtree by passing the same key.
Fix the error in the code to insert the key in the right subtree.
elif key > root.key: root.right = insert(root.right, [1])
To insert the new key in the right subtree, the recursive call must pass the key to insert, not the current node's key or subtree.
Fill both blanks to complete the insertion function that returns the updated root.
def insert(root, key): if root is None: return [1](key) if key < root.key: root.left = insert(root.left, key) elif key > root.key: root.right = insert(root.right, key) else: return root return [2]
The function creates a new Node if the root is None. After insertion, it returns the root to maintain the tree structure.
Fill all three blanks to create a dictionary comprehension that maps node keys to their depths if depth is greater than 1.
depths = {node.key: depth for node, depth in nodes if depth [1] 1 and node.key [2] [3]The comprehension filters nodes with depth greater than 1 (> 1) and keys not equal to None (!= None).