Complete the code to print the root node first in a preorder traversal.
def preorder(node): if node is None: return print(node.value) preorder(node.[1]) preorder(node.right)
In preorder traversal, we visit the root node first, then the left subtree, followed by the right subtree. So, the first recursive call should be on the left child.
Complete the code to traverse the left subtree first in an inorder traversal.
def inorder(node): if node is None: return inorder(node.[1]) print(node.value) inorder(node.right)
In inorder traversal, we visit the left subtree first, then the root node, and finally the right subtree. So, the first recursive call should be on the left child.
Fix the error in the postorder traversal code to visit the right subtree last.
def postorder(node): if node is None: return postorder(node.left) postorder(node.[1]) print(node.value)
In postorder traversal, we visit the left subtree, then the right subtree, and finally the root node. The print statement should come after both recursive calls. The blank should be filled with 'right' to visit the right subtree last.
Fill both blanks to complete the inorder traversal that visits left subtree, root, then right subtree.
def inorder(node): if node is None: return inorder(node.[1]) print(node.[2]) inorder(node.right)
In inorder traversal, we first visit the left child, then print the node's value, and finally visit the right child. So, the first blank is 'left' and the second blank is 'value'.
Fill all three blanks to complete the preorder traversal visiting root, left subtree, then right subtree.
def preorder(node): if node is None: return print(node.[1]) preorder(node.[2]) preorder(node.[3])
In preorder traversal, we print the root node's value first, then recursively traverse the left subtree, followed by the right subtree. So the blanks are 'value', 'left', and 'right' respectively.