Tree Traversal Inorder Left Root Right in DSA Java - 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.
public void inorderTraversal(TreeNode node) {
if (node == null) return;
inorderTraversal(node.left);
System.out.print(node.val + " ");
inorderTraversal(node.right);
}
This code visits each node in a 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 tree grows, the number of nodes increases, and the traversal visits each node once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of steps grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to complete the traversal grows linearly with the number of nodes in the tree.
[X] Wrong: "The traversal takes more time because it calls itself multiple times on the same nodes."
[OK] Correct: Each node is visited exactly once, so no repeated work happens on the same node.
Knowing how tree traversal scales helps you explain your code clearly and shows you understand how recursion works on trees.
"What if we changed inorder traversal to preorder traversal? 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 AQuick Check:
Inorder = Left, Root, Right [OK]
- Confusing inorder with preorder or postorder
- Visiting root before left subtree
- Visiting right subtree before root
root?Solution
Step 1: Check the order of recursive calls
Inorder traversal visits left subtree first, then root, then right subtree.Step 2: Verify method matches inorder order
void inorderTraversal(Node root) { if(root != null) { inorderTraversal(root.left); System.out.print(root.data); inorderTraversal(root.right); } } calls inorderTraversal on left, prints root data, then calls inorderTraversal on right, matching inorder traversal.Final Answer:
Method with left call, print root, then right call -> Option DQuick Check:
Left, Root, Right order in code [OK]
- Printing root before left subtree
- Calling right subtree before left subtree
- Missing null check before recursion
4 / \ 2 5 / \ 1 3
What is the output of an inorder traversal?
Solution
Step 1: Traverse left subtree of root (4)
Left subtree is rooted at 2, which has left child 1 and right child 3. Inorder traversal of left subtree is 1, 2, 3.Step 2: Visit root and right subtree
After left subtree, visit root 4, then right subtree 5.Final Answer:
1 2 3 4 5 -> Option AQuick Check:
Inorder visits nodes in sorted order for BST [OK]
- Visiting root before left subtree
- Mixing left and right subtree order
- Skipping leaf nodes
void inorder(Node root) {
if(root == null) return;
inorder(root.right);
System.out.print(root.data + " ");
inorder(root.left);
}What is the problem with this code?
Solution
Step 1: Analyze the recursive call order
The method calls inorder on right subtree first, then prints root, then calls inorder on left subtree.Step 2: Compare with inorder traversal definition
Standard inorder is Left, Root, Right. This code visits Right, Root, Left, which is reverse inorder.Final Answer:
Visits nodes in reverse inorder (Right, Root, Left) -> Option CQuick Check:
Right, Root, Left order detected [OK]
- Assuming it is correct inorder
- Ignoring order of recursive calls
- Confusing traversal types
Solution
Step 1: Understand inorder traversal on non-BST
Inorder traversal visits nodes in Left, Root, Right order but does not guarantee sorted order if tree is not BST.Step 2: Ensure ascending order output
To get ascending order, collect all node values during traversal, then sort them before printing.Final Answer:
Perform inorder traversal and then sort the collected values before printing -> Option BQuick Check:
Non-BST inorder not sorted; sorting needed [OK]
- Assuming inorder traversal always sorts
- Printing values during traversal without sorting
- Using preorder or postorder expecting sorted output
Ready to prove it?
Pass the test and this pattern moves unknown
