Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Tree Traversal Preorder Root Left Right in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Preorder traversal output of a simple binary tree
What is the output of the preorder traversal (Root, Left, Right) of this binary tree?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number, left: Node | null = null, right: Node | null = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(1, new Node(2), new Node(3));

function preorder(node: Node | null, result: number[] = []): number[] {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root));
A[1, 2, 3]
B[2, 1, 3]
C[3, 2, 1]
D[1, 3, 2]
Attempts:
2 left
💡 Hint
Remember preorder means visit root first, then left subtree, then right subtree.
❓ Predict Output
intermediate
2:00remaining
Preorder traversal output with deeper tree
What is the output of the preorder traversal (Root, Left, Right) of this binary tree?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number, left: Node | null = null, right: Node | null = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(10, new Node(5, new Node(3), new Node(7)), new Node(15, null, new Node(18)));

function preorder(node: Node | null, result: number[] = []): number[] {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root));
A[5, 3, 7, 10, 15, 18]
B[3, 5, 7, 10, 15, 18]
C[10, 15, 18, 5, 3, 7]
D[10, 5, 3, 7, 15, 18]
Attempts:
2 left
💡 Hint
Start from root 10, then go left subtree fully before right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in preorder traversal implementation
This preorder traversal function is supposed to return an array of node values in preorder. What error will it cause when run?
DSA Typescript
function preorder(node: Node | null, result: number[] = []): number[] {
  if (!node) return result;
  preorder(node.left, result);
  result.push(node.value);
  preorder(node.right, result);
  return result;
}
AIt will cause a stack overflow error due to infinite recursion.
BIt will cause a runtime error because result is undefined.
CThe output array will be in inorder traversal order, not preorder.
DThe output array will be empty because values are never added.
Attempts:
2 left
💡 Hint
Check the order in which node values are added to the result array.
🧠 Conceptual
advanced
1:00remaining
Number of nodes visited in preorder traversal
Given a binary tree with 7 nodes, how many nodes does the preorder traversal visit?
A7
B6
C8
DDepends on the tree structure
Attempts:
2 left
💡 Hint
Preorder traversal visits every node exactly once.
🚀 Application
expert
3:00remaining
Preorder traversal output of an unbalanced tree
What is the preorder traversal output of this unbalanced binary tree?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number, left: Node | null = null, right: Node | null = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(8, new Node(3, new Node(1), null), new Node(10, null, new Node(14, new Node(13), null)));

function preorder(node: Node | null, result: number[] = []): number[] {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root));
A[1, 3, 8, 10, 14, 13]
B[8, 3, 1, 10, 14, 13]
C[8, 10, 14, 13, 3, 1]
D[8, 3, 10, 1, 14, 13]
Attempts:
2 left
💡 Hint
Follow root, then left subtree, then right subtree carefully.

Practice

(1/5)
1. What is the order of nodes visited in a preorder tree traversal?
easy
A. Root, Left, Right
B. Left, Root, Right
C. Left, Right, Root
D. Right, Left, Root

Solution

  1. Step 1: Understand preorder traversal definition

    Preorder traversal visits the root node first, then recursively visits the left subtree, followed by the right subtree.
  2. Step 2: Match the order with options

    The order Root, Left, Right matches the preorder traversal definition exactly.
  3. Final Answer:

    Root, Left, Right -> Option A
  4. Quick Check:

    Preorder = Root, Left, Right [OK]
Hint: Preorder always starts with the root node first [OK]
Common Mistakes:
  • Confusing preorder with inorder or postorder
  • Visiting left or right before root
  • Mixing up traversal orders
2. Which of the following TypeScript function signatures correctly represents a preorder traversal function for a binary tree node?
easy
A. function preorder(node: TreeNode): number[]
B. function preorder(node: TreeNode | null): void
C. function preorder(): TreeNode
D. function preorder(node: TreeNode, result: number[]): number

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    function preorder(node: TreeNode | null): void -> Option B
  4. Quick Check:

    Traversal function signature with node or null and void return [OK]
Hint: Traversal functions usually take a node and return void or array [OK]
Common Mistakes:
  • Missing null in node parameter type
  • Wrong return type for traversal
  • Incorrect number of parameters
3. Given the following binary tree:
    1
   / \
  2   3
 / \
4   5

What is the output of a preorder traversal (Root, Left, Right)?
medium
A. [4, 2, 5, 1, 3]
B. [2, 4, 5, 1, 3]
C. [1, 2, 4, 5, 3]
D. [1, 3, 2, 4, 5]

Solution

  1. Step 1: Start at root node 1

    Preorder visits root first: 1
  2. Step 2: Traverse left subtree of 1

    Visit node 2, then its left child 4, then right child 5: 2, 4, 5
  3. Step 3: Traverse right subtree of 1

    Visit node 3: 3
  4. Final Answer:

    [1, 2, 4, 5, 3] -> Option C
  5. Quick Check:

    Preorder = Root, Left, Right = [1, 2, 4, 5, 3] [OK]
Hint: Visit root first, then left subtree, then right subtree [OK]
Common Mistakes:
  • Visiting left subtree after right subtree
  • Starting traversal from left child instead of root
  • Mixing preorder with inorder or postorder
4. Consider this TypeScript preorder traversal function:
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?
medium
A. It does not handle null nodes
B. It will cause infinite recursion
C. It prints nodes in postorder
D. It visits nodes in inorder, not preorder

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It visits nodes in inorder, not preorder -> Option D
  4. Quick Check:

    Print before recursive calls = preorder; here print is in middle [OK]
Hint: Print node value before recursive calls for preorder [OK]
Common Mistakes:
  • Printing node after left subtree instead of before
  • Confusing traversal orders
  • Missing base case for null
5. You want to collect all node values of a binary tree in preorder traversal into an array. Which TypeScript function correctly implements this?
hard
A. 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; }
B. function preorder(node: TreeNode | null): number[] { if (!node) return []; return [...preorder(node.left), node.val, ...preorder(node.right)]; }
C. function preorder(node: TreeNode | null): number[] { if (!node) return []; return [...preorder(node.left), ...preorder(node.right), node.val]; }
D. function preorder(node: TreeNode | null): number[] { if (!node) return []; return [node.val, ...preorder(node.right), ...preorder(node.left)]; }

Solution

  1. Step 1: Understand preorder traversal collection

    Preorder visits root first, then left subtree, then right subtree, collecting values in order.
  2. 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.
  3. 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 A
  4. Quick Check:

    Push root first, then left and right recursively [OK]
Hint: Push root value before recursive calls to left and right [OK]
Common Mistakes:
  • Collecting left subtree before root
  • Mixing left and right subtree order
  • Returning new arrays instead of accumulating
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard