Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Tree Traversal Preorder Root Left Right in DSA Javascript - 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
What is the output of this preorder traversal?
Given the binary tree below, what is the output of the preorder traversal (Root, Left, Right)?
DSA Javascript
class Node {
  constructor(value, left = null, right = 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, result = []) {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root).join(' -> ') + ' -> null');
A10 -> 5 -> 3 -> 7 -> 15 -> 18 -> null
B3 -> 5 -> 7 -> 10 -> 15 -> 18 -> null
C10 -> 15 -> 18 -> 5 -> 3 -> 7 -> null
D5 -> 3 -> 7 -> 10 -> 15 -> 18 -> null
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
❓ Predict Output
intermediate
1:00remaining
Output of preorder traversal on a single-node tree
What is the output of the preorder traversal on this tree with only one node?
DSA Javascript
class Node {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(42);

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

console.log(preorder(root).join(' -> ') + ' -> null');
A42 -> 42 -> null
B42
C42 -> null
Dnull
Attempts:
2 left
💡 Hint
Preorder always includes the root node first.
🔧 Debug
advanced
1:30remaining
Identify the error in this preorder traversal function
What error will this preorder traversal code produce when run?
DSA Javascript
function preorder(node, result = []) {
  if (node == null) return;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

const root = null;
console.log(preorder(root));
ATypeError
B[1]
Cnull
Dundefined
Attempts:
2 left
💡 Hint
Check what happens when the function returns without a value.
❓ Predict Output
advanced
2:00remaining
Preorder traversal output with unbalanced tree
What is the output of the preorder traversal for this unbalanced tree?
DSA Javascript
class Node {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(1,
  new Node(2,
    new Node(4),
    null
  ),
  new Node(3,
    null,
    new Node(5,
      null,
      new Node(6)
    )
  )
);

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

console.log(preorder(root).join(' -> ') + ' -> null');
A1 -> 3 -> 5 -> 6 -> 2 -> 4 -> null
B1 -> 2 -> 4 -> 3 -> 5 -> 6 -> null
C4 -> 2 -> 1 -> 3 -> 5 -> 6 -> null
D1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Attempts:
2 left
💡 Hint
Preorder visits root first, then left subtree, then right subtree.
🧠 Conceptual
expert
1:30remaining
How many nodes are visited in preorder traversal of a full binary tree with height 3?
A full binary tree is a tree where every node has 0 or 2 children. If the tree has height 3 (root is height 1), how many nodes will the preorder traversal visit?
A7
B15
C3
D8
Attempts:
2 left
💡 Hint
Number of nodes in a full binary tree of height h is 2^h - 1.

Practice

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

Solution

  1. Step 1: Understand preorder traversal definition

    Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.
  2. Step 2: Confirm the visiting order

    The visiting order is Root, then Left child, then Right child.
  3. Final Answer:

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

    Preorder = Root, Left, Right [OK]
Hint: Remember preorder starts with the root node first [OK]
Common Mistakes:
  • Confusing preorder with inorder or postorder
  • Visiting left or right child before root
  • Mixing up left and right subtree order
2. Which of the following is the correct JavaScript function signature for preorder traversal of a binary tree node named root?
easy
A. function preorder(root) { if(root) { preorder(root.left); preorder(root.right); console.log(root.value); } }
B. function preorder(root) { if(root) { preorder(root.left); console.log(root.value); preorder(root.right); } }
C. function preorder(root) { if(root) { preorder(root.right); preorder(root.left); console.log(root.value); } }
D. function preorder(root) { if(root) { console.log(root.value); preorder(root.left); preorder(root.right); } }

Solution

  1. Step 1: Recall preorder traversal order

    Preorder visits root first, then left subtree, then right subtree.
  2. 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.
  3. Final Answer:

    function preorder(root) { if(root) { console.log(root.value); preorder(root.left); preorder(root.right); } } -> Option D
  4. Quick Check:

    Print root before children = function preorder(root) { if(root) { console.log(root.value); preorder(root.left); preorder(root.right); } } [OK]
Hint: Print root value before recursive calls for preorder [OK]
Common Mistakes:
  • Printing after visiting children (postorder)
  • Swapping left and right recursive calls
  • Missing base case for null root
3. Given the binary tree below, what is the output of a preorder traversal?
    1
   / \
  2   3
 / \
4   5

Use the format: 1 -> 2 -> 4 -> 5 -> 3 -> null
medium
A. 1 -> 2 -> 4 -> 5 -> 3 -> null
B. 4 -> 2 -> 5 -> 1 -> 3 -> null
C. 2 -> 4 -> 5 -> 1 -> 3 -> null
D. 1 -> 3 -> 2 -> 4 -> 5 -> null

Solution

  1. Step 1: Start at root node 1

    Visit root first: 1
  2. Step 2: Traverse left subtree of 1

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

    Visit 3
  4. Final Answer:

    1 -> 2 -> 4 -> 5 -> 3 -> null -> Option A
  5. Quick Check:

    Preorder visits root, left, right = 1 -> 2 -> 4 -> 5 -> 3 -> null [OK]
Hint: Follow root, then left subtree, then right subtree order [OK]
Common Mistakes:
  • Visiting left subtree after right subtree
  • Starting traversal from left child instead of root
  • Confusing preorder with inorder or postorder
4. The following JavaScript code is intended to perform a preorder traversal and print node values. What is the error?
function preorder(root) {
  if (root === null) return;
  preorder(root.left);
  console.log(root.value);
  preorder(root.right);
}
medium
A. The console.log should be before the recursive calls
B. The base case should check for undefined, not null
C. The function should return a value instead of void
D. The left and right recursive calls are swapped

Solution

  1. Step 1: Analyze the order of operations

    The code visits left child first, then prints root, then visits right child.
  2. Step 2: Compare with preorder traversal order

    Preorder requires printing root before visiting children, so console.log should be before recursive calls.
  3. Final Answer:

    The console.log should be before the recursive calls -> Option A
  4. Quick Check:

    Print root first in preorder [OK]
Hint: Print root value before recursive calls in preorder [OK]
Common Mistakes:
  • Printing after left subtree instead of before
  • Confusing preorder with inorder traversal
  • Ignoring base case for null nodes
5. You have a binary tree where some nodes have only one child. How does preorder traversal handle nodes with a missing left child?
Consider this tree:
    10
      \
      20
     /  \
   30    40

What is the preorder traversal output?
hard
A. 10 -> 40 -> 20 -> 30 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 40 -> 30 -> null
D. 10 -> 30 -> 20 -> 40 -> null

Solution

  1. Step 1: Visit root node 10

    Start with root: 10
  2. Step 2: Check left child of 10

    Left child is missing, so skip to right child 20
  3. Step 3: Visit node 20 and its children

    Visit 20, then left child 30, then right child 40
  4. Final Answer:

    10 -> 20 -> 30 -> 40 -> null -> Option B
  5. Quick Check:

    Preorder visits root, left (if any), right = 10 -> 20 -> 30 -> 40 -> null [OK]
Hint: Skip missing children, visit existing nodes in preorder order [OK]
Common Mistakes:
  • Assuming missing left child means skip right child
  • Visiting children in wrong order
  • Confusing preorder with other traversals
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