Introductions
Tree traversal means visiting the nodes of a tree in a specific order. In this chapter, you will practice the most important traversal techniques used with binary trees: Preorder, Inorder, Postorder, and Level Order Traversal. The questions focus on actually finding traversal sequences and writing simple JavaScript solutions. Each example is explained step by step so beginners can understand how traversal works. Data Structure Tree Traversal Practice questions with solutions help to understand the concepts.
Question 1: Perform Preorder Traversal
Question
Find the preorder traversal of this binary tree:
10
/ \
20 30
/ \ / \
40 50 60 70
Solution
Preorder traversal follows:
Root → Left → Right
Start with the root:
10
Then visit the left subtree:
20 → 40 → 50
Then visit the right subtree:
30 → 60 → 70
Therefore:
10 → 20 → 40 → 50 → 30 → 60 → 70
JavaScript solution:
function preorder(root) {
if (root === null) {
return;
}
console.log(root.value);
preorder(root.left);
preorder(root.right);
}
Output
10
20
40
50
30
60
70
Answer
10 → 20 → 40 → 50 → 30 → 60 → 70
Question 2: Perform Inorder Traversal
Question
Find the inorder traversal of this binary tree:
10
/ \
20 30
/ \ / \
40 50 60 70
Solution
Inorder traversal follows:
Left → Root → Right
First process the left subtree of 10.
For node 20:
40 → 20 → 50
Then visit 10.
Finally, process the right subtree:
60 → 30 → 70
Therefore:
40 → 20 → 50 → 10 → 60 → 30 → 70
JavaScript solution:
function inorder(root) {
if (root === null) {
return;
}
inorder(root.left);
console.log(root.value);
inorder(root.right);
}
Output
40
20
50
10
60
30
70
Answer
40 → 20 → 50 → 10 → 60 → 30 → 70
Question 3: Perform Postorder Traversal
Question
Find the postorder traversal of this binary tree:
10
/ \
20 30
/ \ / \
40 50 60 70
Solution
Postorder traversal follows:
Left → Right → Root
First process the left subtree:
40 → 50 → 20
Then process the right subtree:
60 → 70 → 30
Finally, visit the root:
10
Therefore:
40 → 50 → 20 → 60 → 70 → 30 → 10
JavaScript solution:
function postorder(root) {
if (root === null) {
return;
}
postorder(root.left);
postorder(root.right);
console.log(root.value);
}
Output
40
50
20
60
70
30
10
Answer
40 → 50 → 20 → 60 → 70 → 30 → 10
Question 4: Perform Level Order Traversal
Question
Find the level order traversal of this binary tree:
10
/ \
20 30
/ \ / \
40 50 60 70
Solution
Level order traversal visits nodes level by level.
First level:
10
Second level:
20 → 30
Third level:
40 → 50 → 60 → 70
Therefore:
10 → 20 → 30 → 40 → 50 → 60 → 70
A queue is commonly used for level order traversal.
function levelOrder(root) {
if (root === null) {
return;
}
let queue = [root];
while (queue.length > 0) {
let current = queue.shift();
console.log(current.value);
if (current.left !== null) {
queue.push(current.left);
}
if (current.right !== null) {
queue.push(current.right);
}
}
}
Output
10
20
30
40
50
60
70
Answer
10 → 20 → 30 → 40 → 50 → 60 → 70
Question 5: Find Preorder Traversal of an Unbalanced Tree
Question
Find the preorder traversal:
10
/
20
/ \
30 40
/
50
Solution
Remember:
Preorder = Root → Left → Right
Start at 10.
Then move to 20.
From 20, visit its left child 30.
Then visit its right subtree:
40 → 50
Therefore:
10 → 20 → 30 → 40 → 50
JavaScript:
function preorder(root) {
if (root === null) {
return;
}
console.log(root.value);
preorder(root.left);
preorder(root.right);
}
Output
10
20
30
40
50
Answer
10 → 20 → 30 → 40 → 50
Question 6: Find Inorder Traversal of a Binary Search Tree
Question
Find the inorder traversal of this Binary Search Tree:
50
/ \
30 70
/ \ / \
20 40 60 80
Solution
Inorder traversal follows:
Left → Root → Right
Process the left subtree:
20 → 30 → 40
Visit the root:
50
Process the right subtree:
60 → 70 → 80
Therefore:
20 → 30 → 40 → 50 → 60 → 70 → 80
Because this is a Binary Search Tree, the inorder traversal produces the values in sorted order.
JavaScript:
function inorder(root) {
if (root === null) {
return;
}
inorder(root.left);
console.log(root.value);
inorder(root.right);
}
Output
20
30
40
50
60
70
80
Answer
20 → 30 → 40 → 50 → 60 → 70 → 80
Question 7: Store Preorder Traversal in an Array
Question
Instead of printing the preorder traversal, store the result in a JavaScript array.
Use this tree:
10
/ \
20 30
/ \ \
40 50 60
Solution
Create an empty array:
let result = [];
During preorder traversal, add each visited node to the array.
function preorder(root, result) {
if (root === null) {
return;
}
result.push(root.value);
preorder(root.left, result);
preorder(root.right, result);
}
Complete example:
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
let root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.right = new Node(60);
let result = [];
preorder(root, result);
console.log(result);
The nodes are visited in this order:
10 → 20 → 40 → 50 → 30 → 60
Output
[10, 20, 40, 50, 30, 60]
Answer
The preorder traversal stored in the array is:
[10, 20, 40, 50, 30, 60]
Question 8: Find All Traversals of a Binary Tree
Question
Find the preorder, inorder, and postorder traversals of:
1
/ \
2 3
/ \
4 5
Solution
Preorder
Rule:
Root → Left → Right
Result:
1 → 2 → 4 → 5 → 3
Inorder
Rule:
Left → Root → Right
Result:
4 → 2 → 5 → 1 → 3
Postorder
Rule:
Left → Right → Root
Result:
4 → 5 → 2 → 3 → 1
JavaScript:
function preorder(root) {
if (root === null) return;
console.log(root.value);
preorder(root.left);
preorder(root.right);
}
function inorder(root) {
if (root === null) return;
inorder(root.left);
console.log(root.value);
inorder(root.right);
}
function postorder(root) {
if (root === null) return;
postorder(root.left);
postorder(root.right);
console.log(root.value);
}
Output
Preorder:
1 2 4 5 3
Inorder:
4 2 5 1 3
Postorder:
4 5 2 3 1
Answer
Preorder → 1 → 2 → 4 → 5 → 3
Inorder → 4 → 2 → 5 → 1 → 3
Postorder → 4 → 5 → 2 → 3 → 1
Question 9: Count Nodes Visited During Traversal
Question
How many nodes will be visited during preorder traversal of this tree?
15
/ \
10 25
/ \ \
5 12 30
Solution
Preorder traversal is:
Root → Left → Right
Visit:
15
10
5
12
25
30
Now count them:
1. 15
2. 10
3. 5
4. 12
5. 25
6. 30
Therefore, the traversal visits 6 nodes.
We can also count nodes during traversal:
function countVisited(root) {
if (root === null) {
return 0;
}
return 1 +
countVisited(root.left) +
countVisited(root.right);
}
Output
6
Answer
Preorder traversal visits 6 nodes.
The traversal is:
15 → 10 → 5 → 12 → 25 → 30
Question 10: Print Nodes Level by Level
Question
Print each level of this binary tree on a separate line:
10
/ \
20 30
/ \ / \
40 50 60 70
Expected format:
10
20 30
40 50 60 70
Solution
For level-by-level output, we can use a queue.
First add the root:
Queue = [10]
Process 10 and add its children:
Queue = [20, 30]
Process 20 and 30:
Queue = [40, 50, 60, 70]
Process the last level.
JavaScript:
function printLevels(root) {
if (root === null) {
return;
}
let queue = [root];
while (queue.length > 0) {
let levelSize = queue.length;
let level = [];
for (let i = 0; i < levelSize; i++) {
let current = queue.shift();
level.push(current.value);
if (current.left !== null) {
queue.push(current.left);
}
if (current.right !== null) {
queue.push(current.right);
}
}
console.log(level.join(" "));
}
}
Output
10
20 30
40 50 60 70
Answer
The tree contains three levels:
Level 0 → 10
Level 1 → 20 30
Level 2 → 40 50 60 70
Key Takeaways
- Tree traversal means visiting every node in a specific order.
- Preorder follows Root → Left → Right.
- Inorder follows Left → Root → Right.
- Postorder follows Left → Right → Root.
- Level order visits nodes level by level.
- Preorder, inorder, and postorder are commonly implemented using recursion.
- Level order traversal commonly uses a queue.
- Inorder traversal of a valid Binary Search Tree produces sorted values.
- Traversal can be used to print values, store values in arrays, search data, and solve other tree problems.
- The same tree can produce different results depending on the traversal method.
- Understanding traversal is important before learning advanced tree algorithms.
FAQs
1. What is tree traversal?
Tree traversal is the process of visiting the nodes of a tree in a specific order.
2. What are the main types of tree traversal?
The main traversal methods are preorder, inorder, postorder, and level order traversal.
3. What is preorder traversal?
Preorder traversal visits the root first, followed by the left subtree and then the right subtree.
Root → Left → Right
4. What is inorder traversal?
Inorder traversal visits the left subtree first, then the root, and finally the right subtree.
Left → Root → Right
5. What is postorder traversal?
Postorder traversal visits the left subtree, then the right subtree, and finally the root.
Left → Right → Root
6. What is level order traversal?
Level order traversal visits nodes one level at a time, starting from the root. It commonly uses a queue.
7. Which traversal gives sorted data in a Binary Search Tree?
Inorder traversal gives values in sorted order when the tree follows the Binary Search Tree ordering rules.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
