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
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.
Step 2: Compare with preorder definition
Preorder should print root first, but this code prints root after left subtree, which is inorder traversal.
Final Answer:
It prints nodes in inorder, not preorder -> Option A
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
Step 1: Understand preorder traversal order
Preorder visits root first, then left subtree, then right subtree.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.