Complete the code to print the nodes of a binary search tree in sorted order using inorder traversal.
def inorder_traversal(node): if node is not None: inorder_traversal(node.left) print(node[1]) inorder_traversal(node.right)
The inorder traversal visits nodes in the left subtree, then the current node, then the right subtree. To print the node's value, we access the .value attribute.
Complete the code to collect the inorder traversal result in a list instead of printing.
def inorder_collect(node, result): if node is not None: inorder_collect(node.left, result) result.append(node[1]) inorder_collect(node.right, result)
To collect the values in order, append the node's .value attribute to the result list during traversal.
Fix the error in the inorder traversal code that causes incorrect order by completing the blank.
def inorder_traversal(node): if node is not None: inorder_traversal(node.right) print(node[1]) inorder_traversal(node.left)
The inorder traversal must visit the left subtree first, then the node, then the right subtree. The code visits right first, which is incorrect. Fixing the attribute to .value is necessary to print the node's data.
Fill both blanks to create a dictionary comprehension that maps each node's value to its depth during inorder traversal.
depth_map = {node[1]: depth for node, depth in nodes if node[2]!= None instead of is not None which is less preferred.The dictionary keys should be the node's .value. The condition checks that the node is not None using is not None to avoid errors.
Fill all three blanks to create a dictionary comprehension that stores node values as keys and their depths as values, filtering only nodes with values greater than 10.
depth_map = {node[1]: depth for node, depth in nodes if node[2] [3] 10}>= when only greater than is needed.Use .value to access the node's data for keys and in the condition. The comparison operator > filters nodes with values greater than 10.