Tree Traversal Inorder Left Root 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.
Specifically, we ask: How does the time to do an inorder traversal change with the number of nodes?
Analyze the time complexity of the following code snippet.
void inorderTraversal(Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
This code visits each node in the tree in the order: left child, then root, then right child.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Exactly once per node in the tree.
As the number of nodes grows, the function visits each node once, so the work grows 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 time grows linearly with the number of nodes.
Time Complexity: O(n)
This means the time to complete the traversal grows in direct proportion to the number of nodes in the tree.
[X] Wrong: "Since the function calls itself twice, the time is exponential like O(2^n)."
[OK] Correct: Each node is visited only once, so the total work is just one visit per node, not doubling each time.
Understanding this traversal and its time cost helps you explain how tree operations work efficiently, a key skill in many coding tasks.
"What if we changed the traversal to preorder (root, left, right)? How would the time complexity change?"
Practice
Solution
Step 1: Understand inorder traversal definition
Inorder traversal visits nodes in the order: left child, root node, then right child.Step 2: Apply the order to the tree
This means we first visit all nodes in the left subtree, then the root, then all nodes in the right subtree.Final Answer:
Left subtree, Root, Right subtree -> Option AQuick Check:
Inorder = Left, Root, Right [OK]
- Confusing inorder with preorder or postorder
- Visiting root before left subtree
- Visiting right subtree before root
struct Node?Solution
Step 1: Identify correct parameter type for tree traversal
Traversal functions usually take a pointer to the root node to access the tree nodes.Step 2: Check return type and parameter
Traversal functions typically return void as they print or process nodes during traversal, so void return type with pointer parameter is correct.Final Answer:
void inorder(struct Node* root); -> Option DQuick Check:
Traversal uses pointer and void return [OK]
- Passing struct by value instead of pointer
- Using int return type instead of void
- Missing pointer in parameter
2 / \ 1 3
What is the output of the inorder traversal function that prints node values?
Solution
Step 1: Identify inorder traversal order
Inorder visits left subtree, then root, then right subtree.Step 2: Apply inorder to given tree
Left child is 1, root is 2, right child is 3, so output is "1 2 3".Final Answer:
1 2 3 -> Option CQuick Check:
Inorder of 2-node tree = 1 2 3 [OK]
- Printing root before left subtree
- Printing right subtree before root
- Confusing preorder or postorder output
void inorder(struct Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}What is the error if the function prints nodes in preorder instead of inorder?
Solution
Step 1: Understand traversal order in code
Inorder prints after left subtree call and before right subtree call.Step 2: Identify cause of preorder output
If printf is before left subtree call, root prints first, causing preorder output.Final Answer:
The printf statement is before the left subtree call -> Option AQuick Check:
Print before left subtree = preorder [OK]
- Placing print before left subtree call
- Omitting NULL base case
- Forgetting to call right subtree
What is the inorder traversal output of this tree?
Solution
Step 1: Build the BST structure
Insert 5 as root, 3 left of 5, 7 right of 5, 2 left of 3, 4 right of 3, 6 left of 7, 8 right of 7.Step 2: Perform inorder traversal
Inorder visits nodes in ascending order for BST: 2, 3, 4, 5, 6, 7, 8.Final Answer:
2 3 4 5 6 7 8 -> Option BQuick Check:
Inorder of BST = sorted nodes [OK]
- Listing nodes in insertion order
- Reversing the order
- Skipping nodes in traversal
Ready to prove it?
Pass the test and this pattern moves unknown
