Bird
Raised Fist0
DSA Javaprogramming~10 mins

Tree Traversal Preorder Root Left Right in DSA Java - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the root node's value first in preorder traversal.

DSA Java
void preorder(Node node) {
    if (node == null) return;
    System.out.println(node[1]);
}
Drag options to blanks, or click blank then click option'
A.val
B.data
C.value
D.key
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect field names like val or key.
2fill in blank
medium

Complete the code to recursively traverse the left subtree in preorder.

DSA Java
void preorder(Node node) {
    if (node == null) return;
    System.out.println(node.data);
    preorder(node[1]);
}
Drag options to blanks, or click blank then click option'
A.child
B.right
C.left
D.next
Attempts:
3 left
💡 Hint
Common Mistakes
Using .right instead of .left for left subtree traversal.
3fill in blank
hard

Fix the error in the code to correctly traverse the right subtree in preorder.

DSA Java
void preorder(Node node) {
    if (node == null) return;
    System.out.println(node.data);
    preorder(node.left);
    preorder(node[1]);
}
Drag options to blanks, or click blank then click option'
A.right
B.left
C.child
D.next
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on left child twice or using wrong field names.
4fill in blank
hard

Fill both blanks to complete the preorder traversal method that prints root, left, then right.

DSA Java
void preorder(Node node) {
    if (node == null) return;
    System.out.println(node[1]);
    preorder(node[2]);
    preorder(node.right);
}
Drag options to blanks, or click blank then click option'
A.data
B.val
C.left
D.right
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names or missing the left subtree traversal.
5fill in blank
hard

Fill all three blanks to complete the preorder traversal method that prints root, left, then right subtree.

DSA Java
void preorder(Node node) {
    if (node == null) return;
    System.out.println(node[1]);
    preorder(node[2]);
    preorder(node[3]);
}
Drag options to blanks, or click blank then click option'
A.data
B.left
C.right
D.val
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right pointers or using incorrect field names.

Practice

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

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: Match the order with options

    Root, Left, Right matches the order Root, Left, Right exactly.
  3. Final Answer:

    Root, Left, Right -> Option D
  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 before root
  • Visiting right before left
2. Which of the following Java method signatures correctly represents a preorder traversal method for a binary tree node class named Node?
easy
A. void preorderTraversal(Node root) {}
B. int preorderTraversal(Node root) {}
C. Node preorderTraversal() {}
D. void preorderTraversal() {}

Solution

  1. Step 1: Identify correct method signature for preorder traversal

    Preorder traversal usually returns void and takes the root node as input to traverse the tree.
  2. Step 2: Check options for matching signature

    void preorderTraversal(Node root) {} matches void return type and takes Node root as parameter, which is correct.
  3. Final Answer:

    void preorderTraversal(Node root) {} -> Option A
  4. Quick Check:

    Traversal method is void and takes root node [OK]
Hint: Traversal methods usually return void and take root node [OK]
Common Mistakes:
  • Using return type int instead of void
  • Not passing root node as parameter
  • Returning Node instead of void
3. Given the following binary tree:
    1
   / \
  2   3
 / \
4   5

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

Solution

  1. Step 1: Start preorder traversal at root node 1

    Visit 1 first, then move to left subtree.
  2. Step 2: Traverse left subtree of 1: nodes 2, 4, 5

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

    Visit 3 last.
  4. Final Answer:

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

    Preorder = Root, Left, Right = [1, 2, 4, 5, 3] [OK]
Hint: Follow Root, then left subtree, then right subtree order [OK]
Common Mistakes:
  • Mixing preorder with inorder or postorder
  • Visiting right subtree before left
  • Skipping leaf nodes
4. Consider this Java preorder traversal method for a binary tree node class Node:
void preorder(Node root) {
  if (root == null) return;
  preorder(root.left);
  System.out.print(root.data + " ");
  preorder(root.right);
}
What is the error in this code?
medium
A. It prints nodes in inorder, not preorder
B. It causes a NullPointerException
C. It misses the right subtree
D. It prints nodes in postorder

Solution

  1. Step 1: Analyze the order of operations in the method

    The method first recursively visits left subtree, then prints root data, then visits right subtree.
  2. Step 2: Compare with preorder definition

    Preorder should print root first, but this code prints root after left subtree, which is inorder traversal.
  3. Final Answer:

    It prints nodes in inorder, not preorder -> Option A
  4. Quick Check:

    Print before left recursion for preorder [OK]
Hint: Print root before recursive calls for preorder [OK]
Common Mistakes:
  • Printing after left subtree instead of before
  • Confusing preorder with inorder
  • Missing base case for null
5. You want to collect all node values of a binary tree in preorder traversal into a list in Java. Which code snippet correctly does this?
hard
A. void preorder(Node root, List<Integer> list) { if (root == null) return; preorder(root.left, list); list.add(root.data); preorder(root.right, list); }
B. void preorder(Node root, List<Integer> list) { if (root == null) return; list.add(root.data); preorder(root.left, list); preorder(root.right, list); }
C. void preorder(Node root, List<Integer> list) { if (root == null) return; preorder(root.left, list); preorder(root.right, list); list.add(root.data); }
D. void preorder(Node root, List<Integer> list) { if (root == null) return; preorder(root.right, list); list.add(root.data); preorder(root.left, list); }

Solution

  1. Step 1: Understand preorder traversal order

    Preorder visits root first, then left subtree, then right subtree.
  2. Step 2: Check which code adds root data before recursive calls

    void preorder(Node root, List<Integer> list) { if (root == null) return; list.add(root.data); preorder(root.left, list); preorder(root.right, list); } adds root.data to list first, then recursively visits left and right subtrees, matching preorder.
  3. Final Answer:

    void preorder(Node root, List<Integer> list) { if (root == null) return; list.add(root.data); preorder(root.left, list); preorder(root.right, list); } -> Option B
  4. Quick Check:

    Add root before left and right recursion [OK]
Hint: Add root data before recursive calls for preorder list [OK]
Common Mistakes:
  • Adding node data after left subtree (inorder)
  • Adding node data after both subtrees (postorder)
  • Visiting right subtree before left
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