Complete the code to create a new node in the binary tree.
class Node: def __init__(self, data): self.data = data self.left = None self.right = None root = Node([1])
The root node is created with the value 10.
Complete the code to assign the left child of the root node.
root.left = Node([1])The left child of the root is assigned the value 20.
Fix the error in the function to print the bottom view of the binary tree.
def bottom_view(root): if root is None: return hd_node = dict() queue = [(root, 0)] while queue: node, hd = queue.pop(0) hd_node[[1]] = node.data if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) for key in sorted(hd_node): print(hd_node[key], end=' ')
The horizontal distance (hd) is used as the key to update the bottom view map.
Fill both blanks to update the queue with child nodes and their horizontal distances.
if node.left: queue.append((node.left, hd [1] 1)) if node.right: queue.append((node.right, hd [2] 1))
Left child horizontal distance is one less (hd - 1), right child is one more (hd + 1).
Fill all three blanks to create a dictionary comprehension for bottom view filtering.
bottom_view_map = [1]: [2] for [3], [2] in hd_node.items()}
The dictionary comprehension uses key and value from hd_node.items() to build the bottom view map.
