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 == NULL) return;
printf("%d ", root->data); // Visit root
preorderTraversal(root->left); // Traverse left subtree
preorderTraversal(root->right); // Traverse right subtree
}
This code visits every 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 (printing data).
- How many times: Exactly once per node in the tree.
Each node is visited once, so the total steps grow directly with the number of nodes.
| 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 finish grows directly with the number of nodes in the tree.
[X] Wrong: "Preorder traversal takes longer because it visits the root first and then children."
[OK] Correct: The order of visiting nodes does not change the total number of nodes visited; every node is still visited once.
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 as defined.Final Answer:
Root, Left, Right -> Option AQuick Check:
Preorder = Root, Left, Right [OK]
- Confusing preorder with inorder or postorder
- Swapping left and right subtree order
- Thinking traversal starts with left child
root?Solution
Step 1: Check function parameter type
Binary tree nodes are usually passed as pointers, soNode *rootis correct.Step 2: Check null pointer check syntax
The conditionif(root == NULL)correctly checks for null pointer. Assignment=is wrong.Final Answer:
void preorder(Node *root) { if(root == NULL) return; ... } -> Option CQuick Check:
Pointer param + null check == void preorder(Node *root) { if(root == NULL) return; ... } [OK]
- Passing node by value instead of pointer
- Using assignment '=' instead of comparison '=='
- Incorrect pointer syntax like 'Node root*'
1
/ \
2 3
/ \
4 5
Solution
Step 1: Start at root node 1
Preorder visits root first: 1Step 2: Traverse left subtree of 1
Visit 2, then left child 4, then right child 5: 2, 4, 5Step 3: Traverse right subtree of 1
Visit right child 3: 3Final Answer:
[1, 2, 4, 5, 3] -> Option AQuick Check:
Preorder = Root, Left, Right = [1,2,4,5,3] [OK]
- Mixing preorder with inorder or postorder
- Visiting children before root
- Swapping left and right subtree order
void preorder(Node *root) {
if(root == NULL) return;
preorder(root->left);
preorder(root->right);
printf("%d ", root->data);
}
Solution
Step 1: Analyze traversal order in code
Current code prints after left and right calls, which is postorder (Left, Right, Root).Step 2: Fix order to preorder
Print root data before recursive calls to get Root, Left, Right order.Final Answer:
Move printf before recursive calls to print root first -> Option DQuick Check:
Preorder prints root before children [OK]
- Printing after recursive calls (postorder)
- Using assignment '=' instead of comparison '=='
- Changing return type unnecessarily
void preorder(Node *root, int *arr, int *index) {
if(root == NULL) return;
// What goes here?
}
Solution
Step 1: Understand preorder traversal order
Preorder visits root first, so store root data before recursive calls.Step 2: Correctly update array and index
Use arr[(*index)++] = root->data; to store and increment index, then recurse left and right.Final Answer:
arr[(*index)++] = root->data; preorder(root->left, arr, index); preorder(root->right, arr, index); -> Option BQuick Check:
Store root first, then left and right [OK]
- Storing data after recursive calls (not preorder)
- Incrementing index incorrectly
- Swapping left and right recursive calls
Ready to prove it?
Pass the test and this pattern moves unknown
