A binary search tree contains the value . The tree is traversed in pre-order and the values are printed out. Which of the following sequences is a valid output?
View worked solution
Answer: D
For any node in a Binary Search Tree (BST), the pre-order traversal follows the Root Left Right strategy. This imposes a strict partitioning rule on the pre-order sequence:
- The first element is the root .
- All elements in the sequence following that are smaller than (representing the left subtree) must appear before any elements that are larger than (representing the right subtree).
- This property must hold recursively for every subtree.
Let's analyze the options with the keys :
-
- For root , the left subtree is and the right subtree is .
- For the right subtree root , the elements are . Here, (smaller than ) appears after (larger than ). This violates the BST property. Thus, this sequence is invalid.
-
- For root , the element (smaller than ) appears after (larger than ). This violates the partition property for root . Thus, this sequence is invalid.
-
- For root , the left subtree sequence is .
- For subtree root , the element (smaller than ) appears after (larger than ). This violates the partition property for root . Thus, this sequence is invalid.
-
- For root , the left subtree is and the right subtree is .
- For subtree root , the left subtree is and the right subtree is . This is valid. For root , the right child is . This is valid.
- For subtree root , the left subtree is and the right subtree is . This is valid.
- Since all recursive partitions are valid, this sequence is valid.
Free practice · Your answers on this page are not saved.
