Below is the code I am working on. Error message pops up, and I don’t quite get the error message. Can anyone explain it to me? As far as I know “current_node” is a node and not a list.
URL is below.
# Define your "TreeNode" Python class below class TreeNode: def __init__(self, value): self.value = value self.children =  def add_child(self, child_node): print("Adding " + child_node.value) self.children.append(child_node) def remove_child(self, child_node): print("Removing " + child_node.value + " from " + self.value) self.children = [child for child in self.children if child is not child_node] def traverse(self): nodes_to_visit = [self] while 0 < len(nodes_to_visit): #current_node, an object of class Node current_node = nodes_to_visit.pop() print("Traversing...") print(current_node.value) nodes_to_visit.append(current_node.children) root = TreeNode("CEO") first_child = TreeNode("Vice-President") second_child = TreeNode("Head of Marketing") third_child = TreeNode("Marketing Assistant") root.add_child(first_child) root.add_child(second_child) second_child.add_child(third_child) root.traverse()
You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility
When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer!