Complete the code to create a root node for a tree.
class Node: def __init__(self, value): self.value = value self.children = [] root = Node([1])
The root node should have a value, here we use the string 'root' to identify it clearly.
Complete the code to add a child node to the root.
child = Node('child1') root.children.[1](child)
insert without indexadd which is not a list methodUse append to add a single child node to the children list.
Fix the error in the code to print all children values of the root node.
for child in root.[1]: print(child.value)
child which is a variable, not an attributechilds which is not definedThe list of child nodes is stored in children, so we iterate over root.children.
Fill both blanks to check if a node has children and print their values.
if len(node.[1]) [2] 0: for c in node.children: print(c.value)
== which only checks for exactly zero children< which checks for less than zero childrenWe check if the length of children is greater than zero to know if the node has children.
Fill all three blanks to create a dictionary of node values and their number of children if children exist.
result = [1]: len(node.[2]) for node in nodes if len(node.children) [3] 0
nodes instead of children== instead of > in the conditionThis dictionary comprehension maps each node's value to the count of its children, only if it has children.
