Tree Traversal Preorder Root Left 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 we do a preorder traversal (root, then left, then right)?
Analyze the time complexity of the following code snippet.
public void preorderTraversal(TreeNode node) {
if (node == null) return;
System.out.println(node.val); // Visit root
preorderTraversal(node.left); // Traverse left subtree
preorderTraversal(node.right); // Traverse right subtree
}
This code visits each node in the tree starting from the root, then left child, then right child recursively.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once (printing its value).
- 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 directly in proportion to the number of nodes.
[X] Wrong: "Preorder traversal takes more time because it visits the root first and then both subtrees separately."
[OK] Correct: Each node is visited exactly once regardless of order, so the total time depends only on the number of nodes, not the order.
Understanding preorder traversal time helps you explain how tree algorithms scale, a key skill in many coding challenges and interviews.
"What if we changed preorder traversal to inorder traversal? How would the time complexity change?"
Practice
Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits 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.Final Answer:
Root, Left, Right -> Option DQuick Check:
Preorder = Root, Left, Right [OK]
- Confusing preorder with inorder or postorder
- Visiting left before root
- Visiting right before left
Node?Solution
Step 1: Identify correct method signature for preorder traversal
Preorder traversal usually returns void and takes the root node as input to traverse the tree.Step 2: Check options for matching signature
void preorderTraversal(Node root) {} matches void return type and takes Node root as parameter, which is correct.Final Answer:
void preorderTraversal(Node root) {} -> Option AQuick Check:
Traversal method is void and takes root node [OK]
- Using return type int instead of void
- Not passing root node as parameter
- Returning Node instead of void
1 / \ 2 3 / \ 4 5
What is the output of a 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: nodes 2, 4, 5
Visit 2, then 4 (left child), then 5 (right child).Step 3: Traverse right subtree of 1: node 3
Visit 3 last.Final Answer:
[1, 2, 4, 5, 3] -> Option CQuick Check:
Preorder = Root, Left, Right = [1, 2, 4, 5, 3] [OK]
- Mixing preorder with inorder or postorder
- Visiting right subtree before left
- Skipping leaf nodes
Node:
void preorder(Node root) {
if (root == null) return;
preorder(root.left);
System.out.print(root.data + " ");
preorder(root.right);
}
What is the error in this code?Solution
Step 1: Analyze the order of operations in the method
The method first recursively visits left subtree, then prints root data, then visits right subtree.Step 2: Compare with preorder definition
Preorder should print root first, but this code prints root after left subtree, which is inorder traversal.Final Answer:
It prints nodes in inorder, not preorder -> Option AQuick Check:
Print before left recursion for preorder [OK]
- Printing after left subtree instead of before
- Confusing preorder with inorder
- Missing base case for null
Solution
Step 1: Understand preorder traversal order
Preorder visits root first, then left subtree, then right subtree.Step 2: Check which code adds root data before recursive calls
void preorder(Node root, List<Integer> list) { if (root == null) return; list.add(root.data); preorder(root.left, list); preorder(root.right, list); } adds root.data to list first, then recursively visits left and right subtrees, matching preorder.Final Answer:
void preorder(Node root, List<Integer> list) { if (root == null) return; list.add(root.data); preorder(root.left, list); preorder(root.right, list); } -> Option BQuick Check:
Add root before left and right recursion [OK]
- Adding node data after left subtree (inorder)
- Adding node data after both subtrees (postorder)
- Visiting right subtree before left
Ready to prove it?
Pass the test and this pattern moves unknown
