Tree Traversal Preorder Root Left 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 the tree has more nodes?
Analyze the time complexity of the following code snippet.
function preorderTraversal(node) {
if (node === null) return;
console.log(node.value); // 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.
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.
As the number of nodes increases, the steps increase directly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of steps grows linearly 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: "Preorder traversal takes more time because it visits the root first and then children."
[OK] Correct: The order of visiting nodes does not add extra steps; each node is still visited once, so time depends only on total nodes.
Understanding preorder traversal time helps you explain how tree algorithms scale, a key skill in many coding challenges.
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: Confirm the visiting order
The visiting order is Root, then Left child, then Right child.Final Answer:
Root, Left, Right -> Option CQuick Check:
Preorder = Root, Left, Right [OK]
- Confusing preorder with inorder or postorder
- Visiting left or right child before root
- Mixing up left and right subtree order
root?Solution
Step 1: Recall preorder traversal order
Preorder visits root first, then left subtree, then right subtree.Step 2: Check function steps
function preorder(root) { if(root) { console.log(root.value); preorder(root.left); preorder(root.right); } } prints root value first, then recursively visits left and right children, matching preorder.Final Answer:
function preorder(root) { if(root) { console.log(root.value); preorder(root.left); preorder(root.right); } } -> Option DQuick Check:
Print root before children = function preorder(root) { if(root) { console.log(root.value); preorder(root.left); preorder(root.right); } } [OK]
- Printing after visiting children (postorder)
- Swapping left and right recursive calls
- Missing base case for null root
1 / \ 2 3 / \ 4 5
Use the format:
1 -> 2 -> 4 -> 5 -> 3 -> nullSolution
Step 1: Start at root node 1
Visit root first: 1Step 2: Traverse left subtree of 1
Visit 2, then its left child 4, then right child 5Step 3: Traverse right subtree of 1
Visit 3Final Answer:
1 -> 2 -> 4 -> 5 -> 3 -> null -> Option AQuick Check:
Preorder visits root, left, right = 1 -> 2 -> 4 -> 5 -> 3 -> null [OK]
- Visiting left subtree after right subtree
- Starting traversal from left child instead of root
- Confusing preorder with inorder or postorder
function preorder(root) {
if (root === null) return;
preorder(root.left);
console.log(root.value);
preorder(root.right);
}Solution
Step 1: Analyze the order of operations
The code visits left child first, then prints root, then visits right child.Step 2: Compare with preorder traversal order
Preorder requires printing root before visiting children, so console.log should be before recursive calls.Final Answer:
The console.log should be before the recursive calls -> Option AQuick Check:
Print root first in preorder [OK]
- Printing after left subtree instead of before
- Confusing preorder with inorder traversal
- Ignoring base case for null nodes
Consider this tree:
10
\
20
/ \
30 40What is the preorder traversal output?
Solution
Step 1: Visit root node 10
Start with root: 10Step 2: Check left child of 10
Left child is missing, so skip to right child 20Step 3: Visit node 20 and its children
Visit 20, then left child 30, then right child 40Final Answer:
10 -> 20 -> 30 -> 40 -> null -> Option BQuick Check:
Preorder visits root, left (if any), right = 10 -> 20 -> 30 -> 40 -> null [OK]
- Assuming missing left child means skip right child
- Visiting children in wrong order
- Confusing preorder with other traversals
Ready to prove it?
Pass the test and this pattern moves unknown
