Tree Traversal Basics Quiz
40 comprehensive questions exploring fundamental tree traversal algorithms — with 16 code examples covering inorder, preorder, postorder, and level-order traversals in this cpp quiz.
Question 1
What is tree traversal?
Question 2
In in-order traversal of a binary tree, what is the visiting order?
In-order: Left -> Root -> Right
A
/ \
B C
/ \
D E
Result: D B E A CQuestion 3
What is pre-order traversal?
Question 4
In post-order traversal, when is the root visited?
Post-order: Left -> Right -> Root
A
/ \
B C
Result: B C AQuestion 5
What is level-order traversal?
Question 6
Which traversal gives nodes in sorted order for a binary search tree?
BST: 5
/ \
3 7
/ \
2 4Question 7
What is the time complexity of tree traversal algorithms?
Question 8
In recursive tree traversal, what data structure is implicitly used?
void inorder(Node* root) {
if (root) {
inorder(root->left);
cout << root->val;
inorder(root->right);
}
}Question 9
What is the space complexity of recursive in-order traversal?
Question 10
How does iterative level-order traversal work?
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* curr = q.front(); q.pop();
// process curr
if (curr->left) q.push(curr->left);
if (curr->right) q.push(curr->right);
}Question 11
What is the advantage of pre-order traversal for tree copying?
Question 12
In post-order traversal, what operations benefit from this order?
Post-order: delete subtrees before root
Useful for: memory deallocation, expression evaluationQuestion 13
What is the space complexity of iterative level-order traversal?
Question 14
How can you implement iterative in-order traversal?
stack<Node*> s;
Node* curr = root;
while (curr || !s.empty()) {
while (curr) {
s.push(curr);
curr = curr->left;
}
curr = s.top(); s.pop();
// process curr
curr = curr->right;
}Question 15
What traversal order is used in expression tree evaluation?
Question 16
In level-order traversal, how do you process nodes at each level separately?
Use two queues or size tracking:
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for(int i = 0; i < size; i++) {
// process level
}
}Question 17
What is the key difference between depth-first and breadth-first traversals?
Question 18
When would you choose iterative over recursive traversal?
Iterative avoids stack overflow:
For very deep trees (height > 1000)Question 19
What is the output order for pre-order traversal of a specific tree?
Tree: +
/ \
* 3
/ \
2 4
Pre-order: + * 2 4 3Question 20
How does post-order traversal help in calculating directory sizes?
Question 21
What is the relationship between in-order and binary search trees?
BST in-order: always sorted
Non-BST in-order: may not be sortedQuestion 22
In iterative post-order traversal, what data structure combination is typically used?
Question 23
What traversal is used for finding the maximum depth of a tree?
DFS traversals (pre, in, post) can find depth:
int maxDepth(Node* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}Question 24
How does level-order traversal use memory compared to depth-first traversals?
Question 25
What is the significance of traversal order in tree serialization?
Pre-order: root first - good for reconstruction
In-order: sorted - good for BSTs
Post-order: leaves first - good for evaluationQuestion 26
In Morris traversal, how is in-order traversal achieved without stack or recursion?
Question 27
What traversal pattern is used in topological sorting for DAGs?
Topological sort uses post-order like approach:
Process children before parentQuestion 28
How does the choice of traversal affect algorithm time complexity?
Question 29
What is the iterative approach for pre-order traversal?
stack<Node*> s;
s.push(root);
while (!s.empty()) {
Node* curr = s.top(); s.pop();
// process curr
if (curr->right) s.push(curr->right);
if (curr->left) s.push(curr->left);
}Question 30
In which scenario would level-order traversal be most beneficial?
Question 31
What is the space complexity difference between recursive and iterative traversals?
Recursive: O(h) stack space
Iterative: O(w) for level-order, O(h) for DFSQuestion 32
How does in-order traversal help in validating BST properties?
Question 33
What traversal is most suitable for finding all nodes at a specific level?
Level-order naturally groups by level:
Use queue and process level by levelQuestion 34
In threaded binary trees, how does in-order traversal benefit?
Question 35
What is the practical difference between recursive and iterative implementations for large trees?
Recursive: stack overflow risk
Iterative: safe for deep treesQuestion 36
How does post-order traversal support syntax tree evaluation?
Question 37
What is the relationship between tree traversals and tree isomorphism?
Same in-order + pre-order = isomorphic
Different traversals = different structuresQuestion 38
In which data structure application is pre-order traversal commonly used?
Question 39
What makes level-order traversal unique among tree traversals?
Level-order: breadth-first
Others: depth-firstQuestion 40
Considering all tree traversal methods, which fundamental characteristic most determines the choice between recursive and iterative implementations for production systems handling trees of unknown depth and structure?
