Tree Traversal Preorder Root Left Right in DSA Python - Time & Space Complexity
We want to understand how long it takes to visit every node in a tree using preorder traversal.
How does the time grow as the tree gets bigger?
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 preorder(root):
if root is None:
return
print(root.val)
preorder(root.left)
preorder(root.right)
This code visits each node in the tree starting from the root, then left child, then right child.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once and printing its value.
- How many times: Exactly once per node in the tree.
As the number of nodes grows, the number of visits grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows linearly with the number of nodes.
Time Complexity: O(n)
This means the time to traverse grows directly with the number of nodes in the tree.
[X] Wrong: "Preorder traversal takes more time because it visits nodes multiple times."
[OK] Correct: Each node is visited exactly once, so the time grows only with the number of nodes.
Understanding preorder traversal time helps you explain how tree algorithms scale, a key skill in many coding problems.
"What if we changed preorder to inorder traversal? How would the time complexity change?"
Practice
Solution
Step 1: Understand preorder traversal definition
Preorder traversal means visiting the root node first, then the left subtree, and finally the right subtree.Step 2: Match the order with options
Root, Left, Right matches the order Root, Left, Right exactly as defined.Final Answer:
Root, Left, Right -> Option CQuick Check:
Preorder = Root, Left, Right [OK]
- Confusing preorder with inorder or postorder
- Visiting left subtree before root
- Mixing up left and right subtree order
root?Solution
Step 1: Identify function parameters for preorder traversal
The function needs the root node to start traversal, so it should accept one parameter named root.Step 2: Check options for correct syntax
def preorder(root): defines a function with one parameter root, which is correct and simple for preorder traversal.Final Answer:
def preorder(root): -> Option AQuick Check:
Function header with root parameter [OK]
- Adding unnecessary parameters
- Missing the root parameter
- Incorrect function syntax
1 / \ 2 3 / \ 4 5
What is the output of the preorder traversal?
Solution
Step 1: Start preorder traversal at root node 1
Visit 1 first, then move to left subtree.Step 2: Traverse left subtree of 1
Visit 2, then its left child 4, then right child 5.Step 3: Traverse right subtree of 1
Visit 3 last.Final Answer:
[1, 2, 4, 5, 3] -> Option BQuick Check:
Preorder visits root, left, right = [1, 2, 4, 5, 3] [OK]
- Visiting left subtree before root
- Swapping left and right children
- Confusing preorder with inorder or postorder
def preorder(root):
if root is None:
return
print(root.val)
preorder(root.right)
preorder(root.left)What is the error in this code?
Solution
Step 1: Analyze the order of recursive calls
The function calls preorder on root.right before root.left, which reverses the expected left-right order.Step 2: Understand preorder traversal order
Preorder must visit root, then left subtree, then right subtree. This code visits right subtree before left subtree, which is incorrect.Final Answer:
It visits right subtree before left subtree, reversing preorder order -> Option DQuick Check:
Left subtree must come before right in preorder [OK]
- Swapping left and right recursive calls
- Forgetting base case check
- Printing nodes multiple times
root?Solution
Step 1: Understand preorder traversal collection
Preorder visits root first, then left subtree, then right subtree. We must collect values in this order.Step 2: Analyze each option's return order
def preorder(root): if root is None: return [] return [root.val] + preorder(root.left) + preorder(root.right) returns [root.val] + preorder(left) + preorder(right), which matches preorder order correctly.Step 3: Identify errors in other options
def preorder(root): result = [] if root: result.append(root.val) result.extend(preorder(root.left)) result.extend(preorder(root.right)) return result uses result list but calls preorder recursively without passing result, causing empty lists. def preorder(root): result = [] if root: preorder(root.left) result.append(root.val) preorder(root.right) return result appends root after left subtree, which is inorder. def preorder(root): if root is None: return [] return preorder(root.left) + [root.val] + preorder(root.right) places root.val between left and right, which is inorder.Final Answer:
def preorder(root): if root is None: return [] return [root.val] + preorder(root.left) + preorder(root.right) -> Option AQuick Check:
Preorder = root + left + right [OK]
- Appending root after left subtree (inorder)
- Not returning combined list properly
- Using mutable list without passing it recursively
Ready to prove it?
Pass the test and this pattern moves unknown
