Tree traversal orders (pre-, in-, post-order)
Computer Science · Algorithms
Question 1 of 10
0The values 14, 31, 3, 6, 52 are inserted, in this order, into an empty binary search tree (a value smaller than a node goes to its left, a larger value to its right). Which value is visited 2nd in a pre-order traversal? Reminder, pre-order: visit the node, then its left subtree, then its right subtree.
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Tree traversal orders (pre-, in-, post-order) explained
A binary tree is a structure where each node has at most two children, a left child and a right child. A traversal visits every node exactly once, and the three standard orders differ only in when the node itself is visited relative to its two subtrees. Pre-order visits the node, then the left subtree, then the right subtree. In-order visits the left subtree, then the node, then the right subtree. Post-order visits the left subtree, then the right subtree, then the node.
Each subtree is traversed in the same way, so the rules are applied recursively. A useful memory aid is that the name says where the node goes: pre means first, in means in the middle and post means last. For a binary search tree, in-order traversal visits the values in sorted order, which is a common exam point.
To trace a tree, write the three orders for the smallest subtrees first, then combine them.
Worked example
Take a tree with root A. A has a left child B and a right child C. B has children D and E. C has only a right child F.
- Pre-order: A, B, D, E, C, F gives ABDECF.
- In-order: D, B, E, A, C, F gives DBEACF.
- Post-order: D, E, B, F, C, A gives DEBFCA.
Check the in-order result for the subtree at B first: D, then B, then E gives DBE, which fits into the full order.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
The values 25, 41, 17, 46, 38 are inserted, in this order, into an empty binary search tree (a value smaller than a node goes to its left, a larger value to its right). Which value is visited 3rd in a pre-order traversal? Reminder, pre-order: visit the node, then its left subtree, then its right subtree.
Medium
The values 23, 29, 50, 18, 46, 3, 22 are inserted, in this order, into an empty binary search tree (a value smaller than a node goes to its left, a larger value to its right). How many nodes are visited before the node 23 in a pre-order traversal? Reminder, pre-order: visit the node, then its left subtree, then its right subtree.
Hard
The post-order traversal of a binary search tree visits its nodes in this order: 1, 21, 17, 41, 42, 34, 7. Which value is visited 3rd in an in-order traversal? Reminder, in-order: visit the left subtree, then the node, then the right subtree.