The sorted traversal — Aptitude Question
The sorted traversal: Which traversal of a binary search tree visits the keys in ascending sorted order? Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Easy
- Time target: 45 seconds
Question
Which traversal of a binary search tree visits the keys in ascending sorted order?
Options
- Inorder
- Level-order
- Postorder
- Preorder
Show the answer and the worked solution
Correct answer: A. Inorder
Worked solution
- Inorder visits the left subtree, then the node, then the right subtree.
- In a BST every key on the left is smaller than the node and every key on the right is larger.
- Applying that at every node makes the visit sequence sorted from smallest to largest.
Answer: Inorder.
The idea
Inorder traversal of a BST is the cheapest sorted listing you can get from one, which is why an inorder walk is the standard test that a tree really is a BST.
Shortcut. In a BST left < node < right, so visiting left, node, right yields increasing keys, and that is exactly inorder.
Common trap. "Preorder" tempts as the traversal usually listed first and the one that rebuilds a BST, but it starts at the root, which is rarely the smallest key.
All Data Structures & Algorithms questions · placement mock tests