Tree Traversal Basics Quiz

Tree
0 Passed
0% acceptance

40 comprehensive questions exploring fundamental tree traversal algorithms — with 16 code examples covering inorder, preorder, postorder, and level-order traversals in this cpp quiz.

40 Questions
~80 minutes
1

Question 1

What is tree traversal?

A
The process of visiting each node in a tree exactly once in a systematic order, following specific patterns to access all nodes in the tree structure
B
Searching for a specific node
C
Counting the number of nodes
D
Finding the height of the tree
2

Question 2

In in-order traversal of a binary tree, what is the visiting order?

text
In-order: Left -> Root -> Right
     A
    / \
   B   C
  / \
 D   E
Result: D B E A C
A
Left subtree, root, right subtree, processing nodes in non-decreasing order for binary search trees and enabling sorted output from the hierarchical structure
B
Root, left subtree, right subtree
C
Left subtree, right subtree, root
D
Root, right subtree, left subtree
3

Question 3

What is pre-order traversal?

A
A depth-first traversal that visits the root first, then the left subtree, then the right subtree, useful for creating a copy of the tree or prefix notation in expression trees
B
Visits leaves before internal nodes
C
Visits nodes level by level
D
Visits right subtree before left
4

Question 4

In post-order traversal, when is the root visited?

text
Post-order: Left -> Right -> Root
     A
    / \
   B   C
Result: B C A
A
After both left and right subtrees are completely traversed, making it ideal for tree deletion operations where children must be processed before parents
B
Before any subtrees
C
Between left and right subtrees
D
Only if it's a leaf node
5

Question 5

What is level-order traversal?

A
A breadth-first traversal that visits nodes level by level from top to bottom, using a queue to process nodes in order of their depth from the root
B
Visits nodes in sorted order
C
Visits deepest nodes first
D
Visits nodes randomly
6

Question 6

Which traversal gives nodes in sorted order for a binary search tree?

text
BST:     5
       / \
      3   7
     / \
    2   4
A
In-order traversal, visiting nodes in ascending order by processing left subtree, root, right subtree, which maintains the BST ordering property
B
Pre-order traversal
C
Post-order traversal
D
Level-order traversal
7

Question 7

What is the time complexity of tree traversal algorithms?

A
O(n) where n is the number of nodes, as each node must be visited exactly once regardless of the traversal method used
B
O(log n)
C
O(n log n)
D
O(1)
8

Question 8

In recursive tree traversal, what data structure is implicitly used?

cpp
void inorder(Node* root) {
    if (root) {
        inorder(root->left);
        cout << root->val;
        inorder(root->right);
    }
}
A
System call stack, where each recursive call pushes a stack frame containing local variables and return address, simulating the traversal path through the tree
B
Queue
C
Array
D
Linked list
9

Question 9

What is the space complexity of recursive in-order traversal?

A
O(h) where h is the tree height, due to the maximum recursion depth which equals the longest path from root to leaf in the worst case
B
O(n)
C
O(1)
D
O(log n)
10

Question 10

How does iterative level-order traversal work?

cpp
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);
}
A
Uses a queue to process nodes level by level, enqueueing children after processing parent, ensuring breadth-first traversal without recursion
B
Uses a stack for depth-first traversal
C
Processes nodes in random order
D
Requires O(n) extra space always
11

Question 11

What is the advantage of pre-order traversal for tree copying?

A
Creates the root node first, then recursively copies left and right subtrees, maintaining the exact tree structure and node relationships during the copying process
B
Creates leaves first
C
Creates nodes in sorted order
D
Creates nodes level by level
12

Question 12

In post-order traversal, what operations benefit from this order?

text
Post-order: delete subtrees before root
Useful for: memory deallocation, expression evaluation
A
Tree deletion and memory deallocation, where children must be deleted before their parent to avoid memory leaks and dangling pointers in dynamic tree structures
B
Searching operations
C
Sorting operations
D
Level-based operations
13

Question 13

What is the space complexity of iterative level-order traversal?

A
O(w) where w is the maximum width of the tree, as the queue may hold all nodes at the widest level simultaneously during breadth-first processing
B
O(h) where h is height
C
O(1)
D
O(n)
14

Question 14

How can you implement iterative in-order traversal?

cpp
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;
}
A
Using a stack to simulate recursion, pushing all left children first, then processing nodes and moving to right children, avoiding system stack limitations
B
Using only a queue
C
Using no extra data structures
D
Using two stacks
15

Question 15

What traversal order is used in expression tree evaluation?

A
Post-order traversal, evaluating left and right subexpressions before applying the operator at the root, following the postfix notation evaluation pattern
B
In-order traversal
C
Pre-order traversal
D
Level-order traversal
16

Question 16

In level-order traversal, how do you process nodes at each level separately?

cpp
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
    }
}
A
Track the number of nodes at each level using queue size, processing all nodes at current level before moving to the next level in the breadth-first traversal
B
Use recursion
C
Process nodes randomly
D
Use a stack instead
17

Question 17

What is the key difference between depth-first and breadth-first traversals?

A
Depth-first explores as far as possible along each branch before backtracking, while breadth-first explores all nodes at the current level before moving deeper
B
Depth-first uses queues, breadth-first uses stacks
C
Depth-first is faster
D
Breadth-first visits leaves first
18

Question 18

When would you choose iterative over recursive traversal?

text
Iterative avoids stack overflow:
For very deep trees (height > 1000)
A
When tree height is very large, avoiding stack overflow that can occur with deep recursion in systems with limited stack space
B
When tree is very small
C
When memory is unlimited
D
When code simplicity is priority
19

Question 19

What is the output order for pre-order traversal of a specific tree?

text
Tree:     +
       / \
      *   3
     / \
    2   4
Pre-order: + * 2 4 3
A
Root, left subtree, right subtree, producing prefix notation that shows the hierarchical structure with operators appearing before their operands
B
Left subtree, root, right subtree
C
Left subtree, right subtree, root
D
Right subtree, root, left subtree
20

Question 20

How does post-order traversal help in calculating directory sizes?

A
Calculates sizes of subdirectories before the parent directory, allowing bottom-up size accumulation where leaf file sizes are summed up through the hierarchy
B
Calculates root size first
C
Calculates sizes level by level
D
Ignores subdirectory sizes
21

Question 21

What is the relationship between in-order and binary search trees?

text
BST in-order: always sorted
Non-BST in-order: may not be sorted
A
In-order traversal of a BST produces nodes in sorted order, making it the standard way to extract sorted data from binary search tree structures
B
Pre-order produces sorted order
C
Post-order produces sorted order
D
No traversal produces sorted order
22

Question 22

In iterative post-order traversal, what data structure combination is typically used?

A
Two stacks or one stack with modifications, as post-order is more complex to implement iteratively due to the need to visit children before the parent node
B
Only a queue
C
Only one stack
D
No extra data structures
23

Question 23

What traversal is used for finding the maximum depth of a tree?

cpp
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));
}
A
Any depth-first traversal can be used, as they naturally explore the height by recursively finding the maximum depth of left and right subtrees
B
Only level-order traversal
C
Only in-order traversal
D
Traversal doesn't help find depth
24

Question 24

How does level-order traversal use memory compared to depth-first traversals?

A
Level-order uses O(w) space where w is maximum width, while DFS uses O(h) space where h is height, making level-order better for wide trees and DFS better for tall trees
B
Level-order always uses more memory
C
DFS always uses more memory
D
Memory usage is identical
25

Question 25

What is the significance of traversal order in tree serialization?

text
Pre-order: root first - good for reconstruction
In-order: sorted - good for BSTs
Post-order: leaves first - good for evaluation
A
Different orders preserve different properties, with pre-order enabling tree reconstruction, in-order maintaining sorted order, and post-order supporting evaluation operations
B
All orders are equivalent
C
Only one order is useful
D
Traversal order doesn't matter
26

Question 26

In Morris traversal, how is in-order traversal achieved without stack or recursion?

A
Using threaded binary tree technique, temporarily modifying right pointers to create links back to ancestors, enabling O(1) extra space traversal
B
Using a queue
C
Using two stacks
D
Using extra arrays
27

Question 27

What traversal pattern is used in topological sorting for DAGs?

text
Topological sort uses post-order like approach:
Process children before parent
A
Post-order traversal pattern, processing all dependencies before the current node, ensuring that prerequisite tasks are completed before dependent tasks
B
In-order traversal
C
Pre-order traversal
D
Level-order traversal
28

Question 28

How does the choice of traversal affect algorithm time complexity?

A
Traversal choice doesn't affect time complexity for visiting all nodes, as all traversals are O(n), but the order affects when nodes are processed and what information is available at each step
B
Some traversals are faster than others
C
Level-order is always O(log n)
D
DFS traversals are O(n log n)
29

Question 29

What is the iterative approach for pre-order traversal?

cpp
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);
}
A
Using a stack with right child pushed before left child, ensuring left subtree is processed first after the root in the depth-first traversal pattern
B
Using a queue
C
Using no extra space
D
Using two stacks
30

Question 30

In which scenario would level-order traversal be most beneficial?

A
When you need to process nodes by their level or find the shortest path in an unweighted tree, as breadth-first search explores minimum-depth nodes first
B
When you need sorted output
C
When you need to delete the tree
D
When you need to copy the tree
31

Question 31

What is the space complexity difference between recursive and iterative traversals?

text
Recursive: O(h) stack space
Iterative: O(w) for level-order, O(h) for DFS
A
Recursive uses O(h) system stack space, while iterative uses O(h) for DFS and O(w) for BFS, with iterative avoiding stack overflow for very deep trees
B
Recursive always uses less space
C
Iterative always uses less space
D
Space complexity is identical
32

Question 32

How does in-order traversal help in validating BST properties?

A
Produces nodes in sorted order, allowing verification that each node is greater than its predecessor, confirming the BST ordering invariant is maintained
B
Produces nodes in reverse order
C
Produces nodes in random order
D
Cannot validate BST properties
33

Question 33

What traversal is most suitable for finding all nodes at a specific level?

text
Level-order naturally groups by level:
Use queue and process level by level
A
Level-order traversal, as it processes nodes level by level, making it easy to collect or operate on all nodes at the same depth simultaneously
B
In-order traversal
C
Pre-order traversal
D
Post-order traversal
34

Question 34

In threaded binary trees, how does in-order traversal benefit?

A
Threaded pointers enable O(1) access to inorder successor without stack or recursion, making traversals more efficient in memory-constrained environments
B
Threads make traversal slower
C
Threads are not useful for traversal
D
Threads only help pre-order traversal
35

Question 35

What is the practical difference between recursive and iterative implementations for large trees?

text
Recursive: stack overflow risk
Iterative: safe for deep trees
A
Recursive implementations risk stack overflow for very deep trees, while iterative implementations handle arbitrarily deep trees safely using explicit data structures
B
Recursive is always faster
C
Iterative uses more memory
D
No practical difference exists
36

Question 36

How does post-order traversal support syntax tree evaluation?

A
Evaluates operands in subtrees before applying operators at parent nodes, following the postfix evaluation pattern used in expression parsing and compilation
B
Evaluates operators first
C
Evaluates in random order
D
Cannot evaluate expressions
37

Question 37

What is the relationship between tree traversals and tree isomorphism?

text
Same in-order + pre-order = isomorphic
Different traversals = different structures
A
Different traversal sequences can distinguish non-isomorphic trees, while identical traversals for multiple sequences can confirm isomorphism under certain conditions
B
Traversals cannot detect isomorphism
C
All trees have same traversals
D
Only level-order matters for isomorphism
38

Question 38

In which data structure application is pre-order traversal commonly used?

A
File system operations and directory copying, where the root directory structure needs to be created before populating subdirectories with their contents
B
Expression evaluation
C
Finding sorted sequences
D
Memory deallocation
39

Question 39

What makes level-order traversal unique among tree traversals?

text
Level-order: breadth-first
Others: depth-first
A
It is the only breadth-first traversal, processing nodes by level rather than following branches deep into the tree, providing level-by-level access patterns
B
It visits nodes in sorted order
C
It uses recursion only
D
It is the slowest traversal
40

Question 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?

A
Tree depth and system stack limits, where recursive implementations excel for shallow trees with guaranteed maximum depth while iterative approaches provide robustness for arbitrarily deep trees by avoiding stack overflow risks in production environments with varying input characteristics
B
Code simplicity only
C
Memory usage only
D
Traversal speed only

QUIZZES IN Tree