Skip to main content

Preorder and postorder of a BST

Short answer

Insert keys 52,38,40,27,20,66,55,46,60,26,5,68,23,54 in order into an initially empty BST. Preorder (NLR): 52, 38, 27, 20, 5, 26, 23, 40, 46, 66, 55, 54, 60, 68. Postorder (LRN): 5, 23, 26, 20, 27, 46, 40, 38, 54, 60, 55, 68, 66, 52.

Show the sequence of nodes in preorder and postorder traversal in the BST formed by 52, 38, 40, 27, 20, 66, 55, 46, 60, 26, 5, 68, 23, 54.

Build the BST by inserting the keys in the given order; each key goes left if smaller, right if larger.

The BST

52
├─L:38
│  ├─L:27
│  │  └─L:20
│  │     ├─L:5
│  │     └─R:26
│  │        └─L:23
│  └─R:40
│     └─R:46
└─R:66
   ├─L:55
   │  ├─L:54
   │  └─R:60
   └─R:68

Preorder (root, left, right)

52, 38, 27, 20, 5, 26, 23, 40, 46, 66, 55, 54, 60, 68

Postorder (left, right, root)

5, 23, 26, 20, 27, 46, 40, 38, 54, 60, 55, 68, 66, 52

Check: inorder = 5, 20, 23, 26, 27, 38, 40, 46, 52, 54, 55, 60, 66, 68 (sorted), and the root 52 is first in preorder and last in postorder.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 6 questions →