Tree Traversal Inorder Left Root 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 inorderTraversal(node: TreeNode | null): void {
if (node === null) return;
inorderTraversal(node.left);
console.log(node.value);
inorderTraversal(node.right);
}
This code visits each node in the tree in the order: left child, then root, then right child.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls to visit each node once.
- How many times: Once per node in the tree.
Each node is visited exactly one time, 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: "Since the function calls itself twice, the time complexity is exponential like O(2^n)."
[OK] Correct: Each node is visited only once, so the total work is just one visit per node, not doubling each time.
Understanding tree traversal time helps you explain how algorithms handle hierarchical data efficiently, a common skill in many coding challenges.
"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 the root node, and finally the right subtree.Step 2: Match the traversal order to options
Left subtree, Root, Right subtree correctly states the order as Left subtree, Root, Right subtree.Final Answer:
Left subtree, Root, Right subtree -> Option AQuick Check:
Inorder = Left, Root, Right [OK]
- Confusing inorder with preorder or postorder
- Visiting root before left subtree
- Visiting right subtree before root
Solution
Step 1: Analyze function parameters and return type
An inorder traversal function typically takes a node or null as input and performs traversal, often printing or processing nodes without returning a value.Step 2: Check options for correct signature
function inorder(node: TreeNode | null): void accepts a node or null and returns void, which fits traversal that prints or processes nodes.Final Answer:
function inorder(node: TreeNode | null): void -> Option BQuick Check:
Traversal function takes node or null, returns void [OK]
- Omitting null in parameter type
- Returning incorrect types like number or array
- Missing parameter in function signature
2 / \ 1 3What is the output of inorder traversal printing node values?
Solution
Step 1: Visualize the tree and inorder traversal
Inorder visits left subtree, root, then right subtree. For root 2, left child is 1, right child is 3.Step 2: List nodes in inorder
Visit left: 1, then root: 2, then right: 3, so output is [1, 2, 3].Final Answer:
[1, 2, 3] -> Option DQuick Check:
Inorder of 2-node tree = [1, 2, 3] [OK]
- Printing root before left subtree
- Swapping left and right subtree order
- Confusing preorder or postorder output
function inorder(node: TreeNode | null): void {
if (node === null) return;
inorder(node.right);
console.log(node.val);
inorder(node.left);
}What is the main problem with this code?
Solution
Step 1: Analyze traversal order in code
The function calls inorder on right subtree first, then prints root, then calls inorder on left subtree.Step 2: Compare with correct inorder order
Correct inorder is left subtree, root, right subtree. This code reverses left and right, so order is wrong.Final Answer:
It visits right subtree before left, so traversal order is incorrect -> Option CQuick Check:
Inorder must visit left before right [OK]
- Swapping left and right subtree calls
- Forgetting base case for null
- Printing before recursive calls
Solution
Step 1: Understand inorder traversal with array accumulation
We must visit left subtree, then root (push value), then right subtree, passing the same array to collect values.Step 2: Check each option for correct order and array usage
function inorder(node: TreeNode | null, arr: number[]): void { if (!node) return; inorder(node.left, arr); arr.push(node.val); inorder(node.right, arr); } correctly visits left, pushes root value, then visits right, using the same array.Final Answer:
function inorder(node: TreeNode | null, arr: number[]): void { if (!node) return; inorder(node.left, arr); arr.push(node.val); inorder(node.right, arr); } -> Option AQuick Check:
Left, root, right with shared array [OK]
- Pushing before left subtree traversal
- Swapping left and right subtree calls
- Returning new arrays instead of using passed array
Ready to prove it?
Pass the test and this pattern moves unknown
