Complete the code to create the root node of a tree with value 10.
root = TreeNode([1])The root node is created with the value 10, which is the starting point of the tree.
Complete the code to check if a node is a leaf (has no children).
is_leaf = node.left is [1] and node.right is None
A leaf node has no left or right children, so both should be None.
Fix the error in the code to calculate the height of a tree node.
height = 1 + max([1](node.left), height(node.right)) if node else -1
The function calls itself recursively as 'height' to calculate the height of left and right subtrees.
Fill in the blank to calculate the depth of a node in a tree.
def depth(node, root, current_depth=0): if node == root: return current_depth return depth(node, root.parent, current_depth [1] 1)
Depth increases by 1 as we move from the root to the node, so we add 1 to current_depth.
Fill all three blanks to create a dictionary mapping each node's level to a list of node values at that level.
levels = {}
for node in nodes:
level = node.depth
if level not in levels:
levels[[1]] = []
levels[[2]].append([3])We use the node's level as the key and append the node's value to the list at that level.
