Tree Traversal Inorder Left Root Right in DSA Javascript - 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 we do an inorder traversal (left, root, right)?
Analyze the time complexity of the following code snippet.
function inorderTraversal(node) {
if (node === null) return;
inorderTraversal(node.left);
console.log(node.value);
inorderTraversal(node.right);
}
This code visits each node in a binary tree in the order: left child, current node, 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 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 linearly as the tree size grows.
Time Complexity: O(n)
This means the time to complete the traversal grows in direct proportion to the number of nodes.
[X] Wrong: "The traversal takes more time because it calls itself twice each time, so it must be exponential."
[OK] Correct: Each node is visited only once, so the total steps add up to the number of nodes, not more.
Understanding this traversal's time helps you explain how tree algorithms work efficiently and shows you can analyze recursive code clearly.
"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 subtree first, then the root node, and finally the right subtree.Step 2: Match the traversal order to options
Left subtree, Root, Right subtree correctly states the order as Left subtree, Root, Right subtree.Final Answer:
Left subtree, Root, Right subtree -> Option BQuick Check:
Inorder = Left, Root, Right [OK]
- Confusing inorder with preorder or postorder
- Visiting root before left subtree
- Visiting right subtree before root
node?Solution
Step 1: Recall inorder traversal steps in code
Inorder traversal visits left subtree, then root, then right subtree. So the function must call inorder on left, print root, then call inorder on right.Step 2: Check each option's order
function inorder(node) { if(node) { inorder(node.left); console.log(node.value); inorder(node.right); } } matches the correct order: left, root (console.log), right. Others have wrong orders.Final Answer:
function inorder(node) { if(node) { inorder(node.left); console.log(node.value); inorder(node.right); } } -> Option CQuick Check:
Left, Root, Right in code = function inorder(node) { if(node) { inorder(node.left); console.log(node.value); inorder(node.right); } } [OK]
- Printing before left subtree traversal
- Swapping left and right subtree calls
- Missing null check for node
10
/ \
5 15
/ / \
3 12 20
Solution
Step 1: Traverse left subtree of root 10
Left subtree of 10 is rooted at 5. Its left child is 3. Inorder of left subtree: visit 3, then 5 (root of subtree).Step 2: Visit root 10 and then right subtree
After left subtree, visit root 10. Then traverse right subtree rooted at 15: left child 12, root 15, right child 20. So inorder right subtree is 12, 15, 20.Final Answer:
[3, 5, 10, 12, 15, 20] -> Option AQuick Check:
Inorder = Left, Root, Right = [3,5,10,12,15,20] [OK]
- Visiting root before left subtree
- Mixing order of right subtree nodes
- Skipping leaf nodes
function inorder(node) {
if (node === null) return;
console.log(node.value);
inorder(node.left);
inorder(node.right);
}Solution
Step 1: Analyze the order of operations in code
The code prints node.value first, then calls inorder on left and right. This is preorder traversal order.Step 2: Identify correct inorder order
Inorder traversal requires visiting left subtree first, then printing root, then right subtree. The code prints root before left subtree, which is incorrect.Final Answer:
It prints the root before left subtree, so not inorder -> Option AQuick Check:
Print after left call for inorder [OK]
- Printing root before left subtree
- Confusing preorder with inorder
- Not checking for null nodes (though code does here)
function inorder(node, arr) {
if (!node) return;
// Fill missing code here
}Solution
Step 1: Recall inorder traversal order for BST
Inorder traversal visits left subtree, then root, then right subtree, which returns values in ascending order for BST.Step 2: Choose code that collects values in correct order
inorder(node.left, arr); arr.push(node.value); inorder(node.right, arr); calls inorder on left, then pushes current node value, then calls inorder on right, correctly collecting values in ascending order.Final Answer:
inorder(node.left, arr); arr.push(node.value); inorder(node.right, arr); -> Option DQuick Check:
Left, Root, Right with push = inorder(node.left, arr); arr.push(node.value); inorder(node.right, arr); [OK]
- Pushing before left subtree traversal
- Swapping left and right subtree calls
- Pushing after both subtree traversals
Ready to prove it?
Pass the test and this pattern moves unknown
