Tree Traversal Inorder Left Root Right in DSA Python - 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?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def inorder(root):
if root is None:
return
inorder(root.left)
print(root.val)
inorder(root.right)
This code visits each node in a tree in the order: left child, then root, then right child.
- Primary operation: Recursive calls to visit each node once.
- How many times: Once per node in the tree.
Each node is visited exactly 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 complete the traversal grows directly with the number of nodes in the tree.
[X] Wrong: "Inorder traversal takes more time because it visits nodes multiple times."
[OK] Correct: Each node is visited exactly once, so the time grows linearly, not more.
Understanding this helps you explain how tree traversals work efficiently and shows you can analyze recursive code clearly.
"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 subtree first, then root node, then right subtree.Step 2: Apply the order to the tree nodes
This means we first explore all nodes in the left subtree, then the root, then the right subtree.Final Answer:
Left subtree, Root, Right subtree -> Option DQuick 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: Recall inorder traversal order
Inorder visits left subtree, then root, then right subtree.Step 2: Match code to order
def inorder(root): if root: inorder(root.left) print(root.val) inorder(root.right) calls inorder on left, prints root value, then calls inorder on right, matching inorder traversal.Final Answer:
Option A code correctly implements inorder traversal -> Option AQuick Check:
Left, Root, Right order in code [OK]
- Printing root before left subtree call
- Swapping left and right subtree calls
- Missing base case check for None
2
/ \
1 3
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def inorder(root):
if root:
inorder(root.left)
print(root.val, end=' ')
inorder(root.right)
root = Node(2)
root.left = Node(1)
root.right = Node(3)
inorder(root)Solution
Step 1: Trace inorder traversal on given tree
Start at root (2), first visit left subtree (1), print 1, then print root 2, then visit right subtree (3), print 3.Step 2: Write output sequence
The printed sequence is: 1 2 3.Final Answer:
1 2 3 -> Option AQuick Check:
Inorder output = 1 2 3 [OK]
- Printing root before left subtree
- Swapping left and right subtree outputs
- Confusing preorder or postorder output
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def inorder(root):
inorder(root.left)
print(root.val, end=' ')
inorder(root.right)
root = Node(1)
inorder(root)
Solution
Step 1: Analyze base case in inorder function
The function checks root.left and root.right but never checks if root itself is None, which causes errors or no output if root is None.Step 2: Identify missing base case effect
Without checking if root is None, the function may fail or skip printing when called with None, so addingif root is None: returnfixes it.Final Answer:
The base case is missing; should check if root is None first -> Option CQuick Check:
Check root None before recursion [OK]
- Not checking if root is None before accessing attributes
- Incorrect order of print and recursive calls
- Forgetting to create root node before traversal
5
/ \
3 8
\ \
4 9
Options:
A) 3 5 4 8 9
B) 4 3 5 9 8
C) 3 4 5 8 9
D) 5 3 4 8 9Solution
Step 1: Perform inorder traversal on given tree
Start at root (5), visit left subtree: node 3 has no left child, so print 3, then visit right child 4, print 4. Then print root 5. Then visit right subtree: node 8 has no left child, print 8, then right child 9, print 9.Step 2: Write the output sequence
The output is: 3 4 5 8 9.Final Answer:
3 4 5 8 9 -> Option BQuick Check:
Inorder visits left, root, right even if child missing [OK]
- Skipping nodes with only one child
- Printing root before left subtree
- Confusing preorder or postorder output
Ready to prove it?
Pass the test and this pattern moves unknown
