Tree Traversal Preorder Root Left Right in DSA C++ - Time & Space Complexity
We want to understand how the time needed to visit all nodes in a tree grows as the tree gets bigger.
How does the number of steps change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
void preorderTraversal(Node* root) {
if (root == nullptr) return;
visit(root); // Process current node
preorderTraversal(root->left); // Traverse left subtree
preorderTraversal(root->right); // Traverse right subtree
}
This code visits each node in the tree starting from the root, then left child, then right child.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once (the
visit(root)call). - How many times: Exactly once per node in the tree.
As the number of nodes (n) increases, the number of visits grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to complete the traversal grows directly with the number of nodes in the tree.
[X] Wrong: "Preorder traversal takes more time because it visits nodes multiple times."
[OK] Correct: Each node is visited exactly once, so the time depends only on the number of nodes, not repeated visits.
Understanding preorder traversal time helps you explain how tree algorithms scale and shows you can analyze recursive code clearly.
"What if we changed preorder traversal to inorder traversal? How would the time complexity change?"
Practice
Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.Step 2: Match the order with options
Root, Left, Right matches the order Root, Left, Right exactly.Final Answer:
Root, Left, Right -> Option DQuick Check:
Preorder = Root, Left, Right [OK]
- Confusing preorder with inorder or postorder
- Visiting left before root
- Visiting right before left
Node*?Solution
Step 1: Recall preorder traversal order
Preorder visits root first, then left subtree, then right subtree.Step 2: Check function code order
void preorder(Node* root) { if(root) { cout << root->data; preorder(root->left); preorder(root->right); } } prints root data first, then recursively visits left and right children, matching preorder.Final Answer:
void preorder(Node* root) { if(root) { cout << root->data; preorder(root->left); preorder(root->right); } } -> Option AQuick Check:
Print root first, then left, then right [OK]
- Printing after recursive calls (postorder)
- Swapping left and right recursive calls
- Missing null check for root
10
/ \
5 15
/ / \
3 12 20
Solution
Step 1: Start at root and visit nodes in preorder
Visit root 10, then left subtree (5, then 3), then right subtree (15, then 12 and 20).Step 2: Write traversal sequence
Sequence is 10, 5, 3, 15, 12, 20.Final Answer:
[10, 5, 3, 15, 12, 20] -> Option BQuick Check:
Preorder = Root, Left, Right = [10,5,3,15,12,20] [OK]
- Mixing preorder with inorder or postorder
- Visiting right subtree before left
- Skipping leaf nodes
void preorder(Node* root) {
if(root == nullptr) return;
preorder(root->left);
cout << root->data << " ";
preorder(root->right);
}
Solution
Step 1: Identify preorder traversal order
Preorder visits root first, then left subtree, then right subtree.Step 2: Check given function and options
The given function prints after left subtree, which is inorder traversal. void preorder(Node* root) { if(root == nullptr) return; cout << root->data << " "; preorder(root->left); preorder(root->right); } prints root first, then left, then right, matching preorder.Final Answer:
void preorder(Node* root) { if(root == nullptr) return; cout << root->data << " "; preorder(root->left); preorder(root->right); } -> Option AQuick Check:
Print root before recursive calls for preorder [OK]
- Printing after left subtree (inorder)
- Swapping left and right subtree calls
- Missing null pointer check
8
/ \
4 10
\ \
6 20
Solution
Step 1: Understand preorder traversal for serialization
Preorder visits root first, then left subtree, then right subtree, capturing structure including nodes with single children.Step 2: Traverse given tree in preorder
Visit 8 (root), then left subtree: 4, then 6 (right child of 4), then right subtree: 10, then 20 (right child of 10).Step 3: Write traversal sequence
Sequence is [8, 4, 6, 10, 20].Final Answer:
[8, 4, 6, 10, 20] -> Option CQuick Check:
Preorder captures root-left-right including single children [OK]
- Ignoring nodes with single child
- Mixing preorder with inorder
- Visiting right subtree before left
Ready to prove it?
Pass the test and this pattern moves unknown
