---
title: "Binary tree traversals; preorder of BST"
subject: "Data Structure and Algorithm"
chapter: 6
key: "semester_4/data_structure_and_algorithm:binary-tree-traversal-methods-preorder"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/binary-tree-traversal-methods-preorder
site: BCT Engineering
---

# Binary tree traversals; preorder of BST

Data Structure and Algorithm · Chapter 6: Trees

- Asked in: 2070 Bhadra, 3+4 marks
- Video: [Binary tree traversal: Preorder, Inorder, Postorder](https://www.youtube.com/watch?v=gm8DUJJhmY4) — mycodeschool
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/binary-tree-traversal-methods-preorder
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-6

## Question

Explain different traversal methods for a binary tree. What will be the result of pre-order traversal for given binary search tree with nodes: 20, 10, 18, 4, 8, 5, 13, 16, 47, 1, and 27?

## Short answer

Preorder visits root, left subtree, right subtree (NLR); inorder (LNR) gives sorted order for BST; postorder (LRN) visits children before root. For the BST built from 20,10,18,4,8,5,13,16,47,1,27, the preorder sequence is 20, 10, 4, 1, 8, 5, 18, 13, 16, 47, 27.

## Answer

A traversal visits every node of a binary tree exactly once. The three depth-first methods differ in when the root is visited (L = left, R = right, N = node/root).

| Traversal | Order | Rule |
|---|---|---|
| Preorder | N L R | visit root, traverse left subtree, traverse right subtree |
| Inorder | L N R | traverse left subtree, visit root, traverse right subtree |
| Postorder | L R N | traverse left subtree, traverse right subtree, visit root |

```c
void preorder(struct node *p)  { if (p) { printf("%d ", p->info); preorder(p->left); preorder(p->right); } }
void inorder(struct node *p)   { if (p) { inorder(p->left); printf("%d ", p->info); inorder(p->right); } }
void postorder(struct node *p) { if (p) { postorder(p->left); postorder(p->right); printf("%d ", p->info); } }
```
Level-order (breadth-first) traversal using a queue is a fourth method.

Example for tree `A(B,C)`: preorder A B C, inorder B A C, postorder B C A.

#### Preorder of the given BST
Insert 20, 10, 18, 4, 8, 5, 13, 16, 47, 1, 27 in order:

```text
20
├─L:10
│  ├─L:4
│  │  ├─L:1
│  │  └─R:8
│  │     └─L:5
│  └─R:18
│     └─L:13
│        └─R:16
└─R:47
   └─L:27
```
Preorder (root, left, right):

**20, 10, 4, 1, 8, 5, 18, 13, 16, 47, 27**

(Inorder check: 1, 4, 5, 8, 10, 13, 16, 18, 20, 27, 47, which is sorted, so the tree is correct.)

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the order of preorder traversal?
   A. Left, root, right
   B. Root, left subtree, right subtree
   C. Left, right, root
   D. Right, root, left
   Answer: B. N L R: root is visited first. (Key point: Preorder order)

2. Which pair is correct?
   A. Inorder is N L R
   B. Postorder is N L R
   C. Inorder is L R N
   D. Inorder is L N R and postorder is L R N
   Answer: D. The names say when the root is visited. (Key point: Inorder vs postorder)

3. What is the preorder traversal of the given BST?
   A. 1, 4, 5, 8, 10, 13, 16, 18, 20, 27, 47
   B. 20, 10, 4, 1, 8, 5, 18, 13, 16, 47, 27
   C. 20, 10, 47, 4, 18, 27, 1, 8, 13, 5, 16
   D. 1, 5, 8, 4, 16, 13, 18, 10, 27, 47, 20
   Answer: B. Preorder starts at the root 20 then goes down the left subtree before the right. (Key point: Preorder result)

4. Which data structure does level-order traversal use?
   A. A stack
   B. A heap
   C. A linked list of leaves
   D. A queue
   Answer: D. Breadth-first traversal visits nodes level by level using a queue. (Key point: Level-order)

## Related in Data Structure and Algorithm

- [Preorder and postorder of a BST](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abst-preorder-postorder-52-38) — Data Structure and Algorithm, chapter 6
- [Draw binary tree from preorder and inorder](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adraw-tree-from-preorder-inorder) — Data Structure and Algorithm, chapter 6
- [Strictly binary tree has 2n-1 nodes](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astrict-binary-tree-nodes-proof) — Data Structure and Algorithm, chapter 6
- [BST properties and recursive search](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abst-properties-recursive-search) — Data Structure and Algorithm, chapter 6
- [Expression tree and traversals](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aexpression-tree-traversal) — Data Structure and Algorithm, chapter 6
- [B-tree of order 3 for 52, 46, 27, ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-order3-52-46-27) — Data Structure and Algorithm, chapter 6

## Related in other subjects

- [Subnet 10.10.10.0/24 for 5, 16, 23, 27](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Asubnetting-10-10-10-0) — Computer Networks and Security, chapter 4, asked 2×
- [De Morgan's theorem and (-47+27) by 2's complement](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Ademorgan-twos-complement-addition) — Digital Logic, chapter 2
- [Bresenham: line (20,10) to (30,18)](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Adigitize-line-20-10-30-18) — Computer Graphics, chapter 2, asked 4×
- [Decision tree formulas; 20 servers, 10 E](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Adecision-tree-20-servers) — Telecommunications, chapter 6
