Bird
Raised Fist0
DSA Javaprogramming~5 mins

Tree Traversal Preorder Root Left Right in DSA Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is preorder traversal in a binary tree?
Preorder traversal visits nodes in this order: Root first, then Left subtree, and finally Right subtree. It means we process the current node before its children.
Click to reveal answer
beginner
Show the preorder traversal order for this tree:
Root: 1, Left child: 2, Right child: 3
The preorder traversal order is: 1 -> 2 -> 3. We visit root (1), then left child (2), then right child (3).
Click to reveal answer
beginner
What is the first node visited in preorder traversal?
The first node visited is always the root node of the tree or subtree.
Click to reveal answer
intermediate
Why is preorder traversal useful?
Preorder traversal is useful to copy a tree, or to get prefix expression of an expression tree because it processes the root before children.
Click to reveal answer
intermediate
What is the preorder traversal output for this tree?
10
/ \
5 15
/ / \
3 12 20
Preorder traversal visits nodes as: 10 -> 5 -> 3 -> 15 -> 12 -> 20.
Click to reveal answer
In preorder traversal, which node is visited first?
ARoot node
BLeft child
CRight child
DLeaf node
What is the correct order of nodes visited in preorder traversal?
ARoot, Left, Right
BLeft, Root, Right
CLeft, Right, Root
DRight, Left, Root
Which traversal is preorder?
ALeft, Right, Root
BRight, Root, Left
CLeft, Root, Right
DRoot, Left, Right
If a tree has nodes 1 (root), 2 (left), 3 (right), what is preorder traversal?
A2 -> 3 -> 1
B2 -> 1 -> 3
C1 -> 2 -> 3
D3 -> 2 -> 1
Which of these is NOT a use case of preorder traversal?
AGetting prefix expression of expression tree
BFinding the height of a tree
CCopying a tree
DVisiting root before children
Explain how preorder traversal visits nodes in a binary tree.
Think about the order: root, left, right.
You got /4 concepts.
    Given a simple binary tree, describe the steps to perform preorder traversal and list the nodes visited.
    Imagine walking through the tree visiting root first.
    You got /5 concepts.

      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