Tree Traversal Preorder Root Left Right in DSA Typescript - 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(root: TreeNode | null): number[] {
if (root === null) return [];
const left = preorderTraversal(root.left);
const right = preorderTraversal(root.right);
return [root.val, ...left, ...right];
}
This code visits each node in a tree starting from the root, then left child, then right child, collecting values in that order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
Each node is visited exactly once, so the 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 time grows linearly as the tree size increases.
Time Complexity: O(n)
This means the time to complete the traversal grows in direct proportion to the number of nodes.
[X] Wrong: "Preorder traversal takes more than linear time because it visits nodes multiple times."
[OK] Correct: Each node is visited exactly once, so the total steps grow only with the number of nodes, not more.
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
preorder tree traversal?Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits the root node first, then recursively visits the left subtree, followed by the right subtree.Step 2: Match the order with options
The order Root, Left, Right matches the preorder traversal definition exactly.Final Answer:
Root, Left, Right -> Option AQuick Check:
Preorder = Root, Left, Right [OK]
- Confusing preorder with inorder or postorder
- Visiting left or right before root
- Mixing up traversal orders
Solution
Step 1: Analyze function parameters and return type
A preorder traversal function typically takes a node (or null) and returns void if it prints or processes nodes directly.Step 2: Check each option
function preorder(node: TreeNode | null): void accepts a node or null and returns void, which fits a traversal that processes nodes without returning a value.Final Answer:
function preorder(node: TreeNode | null): void -> Option BQuick Check:
Traversal function signature with node or null and void return [OK]
- Missing null in node parameter type
- Wrong return type for traversal
- Incorrect number of parameters
1 / \ 2 3 / \ 4 5
What is the output of a preorder traversal (Root, Left, Right)?
Solution
Step 1: Start at root node 1
Preorder visits root first: 1Step 2: Traverse left subtree of 1
Visit node 2, then its left child 4, then right child 5: 2, 4, 5Step 3: Traverse right subtree of 1
Visit node 3: 3Final Answer:
[1, 2, 4, 5, 3] -> Option CQuick Check:
Preorder = Root, Left, Right = [1, 2, 4, 5, 3] [OK]
- Visiting left subtree after right subtree
- Starting traversal from left child instead of root
- Mixing preorder with inorder or postorder
function preorder(node: TreeNode | null): void {
if (node === null) return;
preorder(node.left);
console.log(node.val);
preorder(node.right);
}What is the error in this code?
Solution
Step 1: Analyze the order of operations in the function
The function first calls preorder on left child, then prints current node, then calls preorder on right child.Step 2: Compare with preorder definition
Preorder should print the node first, then traverse left and right. This code prints after left subtree, so it is inorder traversal.Final Answer:
It visits nodes in inorder, not preorder -> Option DQuick Check:
Print before recursive calls = preorder; here print is in middle [OK]
- Printing node after left subtree instead of before
- Confusing traversal orders
- Missing base case for null
Solution
Step 1: Understand preorder traversal collection
Preorder visits root first, then left subtree, then right subtree, collecting values in order.Step 2: Check each function's order and correctness
function preorder(node: TreeNode | null, result: number[] = []): number[] { if (!node) return result; result.push(node.val); preorder(node.left, result); preorder(node.right, result); return result; } pushes node.val first, then recursively collects left and right, returning the result array correctly.Final Answer:
function preorder(node: TreeNode | null, result: number[] = []): number[] { if (!node) return result; result.push(node.val); preorder(node.left, result); preorder(node.right, result); return result; } -> Option AQuick Check:
Push root first, then left and right recursively [OK]
- Collecting left subtree before root
- Mixing left and right subtree order
- Returning new arrays instead of accumulating
Ready to prove it?
Pass the test and this pattern moves unknown
