Tree Traversal Inorder Left Root Right in DSA C++ - Time & Space Complexity
We want to understand how the time taken 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 == nullptr) return;
inorderTraversal(root->left);
// Process root node here
inorderTraversal(root->right);
}
This code visits each node in the tree in the order: left child, 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.
Each node is visited once, so the total 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 work grows in a straight line as the tree size increases.
Time Complexity: O(n)
This means the time to complete the traversal grows directly in proportion to the number of nodes.
[X] Wrong: "Since recursion calls itself twice, the time doubles each time and becomes exponential."
[OK] Correct: Each node is visited only once, so the total work is just one visit per node, not repeated multiple times.
Understanding this traversal's time complexity helps you explain how tree operations scale, a key skill in many coding challenges.
"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, then root, then right child.Step 2: Apply to binary tree traversal
This means we first visit all nodes in the left subtree, then the root node, then all nodes in the right subtree.Final Answer:
Left subtree, Root, Right subtree -> Option CQuick Check:
Inorder = Left, Root, Right [OK]
- Confusing inorder with preorder or postorder
- Visiting root before left subtree
- Visiting right subtree before root
Node* root?Solution
Step 1: Identify correct parameter type
Traversal functions usually take a pointer to the root node to access the tree nodes.Step 2: Identify correct return type
Traversal functions typically return void because they print or process nodes without returning a value.Final Answer:
void inorderTraversal(Node* root) -> Option DQuick Check:
Pointer parameter and void return [OK]
- Passing node by value instead of pointer
- Using int return type instead of void
- Missing pointer symbol * in parameter
2 / \ 1 3
What is the output of the inorder traversal function that prints node values separated by spaces?
Solution
Step 1: Traverse left subtree
Start at root (2), go left to node (1), print 1 since it has no left child.Step 2: Visit root and right subtree
Return to root (2), print 2, then go right to node (3), print 3.Final Answer:
1 2 3 -> Option AQuick Check:
Inorder traversal output = 1 2 3 [OK]
- Printing root before left subtree
- Printing right subtree before root
- Confusing preorder or postorder output
void inorder(Node* root) {
if (root == nullptr) return;
inorder(root->left);
std::cout << root->val << " ";
inorder(root->right);
}What is the error if the output is empty for a non-empty tree?
Solution
Step 1: Check base case correctness
The base case correctly returns when root is nullptr, so no error here.Step 2: Verify function call
If output is empty, likely the function was never called with the actual root node of the tree.Final Answer:
The function is not called with the root node -> Option BQuick Check:
Missing initial call causes no output [OK]
- Not calling traversal function at all
- Calling with nullptr instead of root
- Assuming print statement missing when it exists
4
/ \
2 6
/ \ / \
1 3 5 7
Using inorder traversal, which sequence of node values will you get?
Solution
Step 1: Traverse left subtree of root (4)
Visit left subtree rooted at 2: inorder visits 1, then 2, then 3.Step 2: Visit root and right subtree
After left subtree, visit root 4, then right subtree rooted at 6: visit 5, then 6, then 7.Final Answer:
[1, 2, 3, 4, 5, 6, 7] -> Option AQuick Check:
Inorder traversal of BST gives sorted sequence [OK]
- Mixing preorder or postorder sequences
- Visiting root before left subtree
- Visiting right subtree before root
Ready to prove it?
Pass the test and this pattern moves unknown
