What is the order of visiting nodes in Preorder Tree Traversal?
In Preorder traversal, nodes are visited in this order: Root first, then Left subtree, and finally Right subtree.
Click to reveal answer
intermediate
Why is Preorder traversal useful in copying a tree?
Preorder traversal visits the root before its children, so it can recreate the tree structure by copying the root node first, then recursively copying left and right subtrees.
Click to reveal answer
intermediate
Rust code snippet: What does this function do?
fn preorder(node: &Option<Box<Node>>) {
if let Some(n) = node {
println!("{}", n.value);
preorder(&n.left);
preorder(&n.right);
}
}
This function performs a Preorder traversal on a binary tree node: it prints the current node's value, then recursively visits the left child, then the right child.
Click to reveal answer
beginner
In Preorder traversal, what happens if a node has no left child?
If a node has no left child, the traversal skips the left subtree and moves directly to visit the right subtree after visiting the root.
Click to reveal answer
beginner
What is the output of Preorder traversal on this tree?
1
/ \
2 3
/ / \
4 5 6
The Preorder traversal visits nodes in Root-Left-Right order:
1 -> 2 -> 4 -> 3 -> 5 -> 6
Click to reveal answer
In Preorder traversal, which node is visited first?
ARoot node
BLeft child
CRight child
DLeaf node
✗ Incorrect
Preorder traversal always visits the root node first before its children.
What is the correct order of nodes visited in Preorder traversal?
ALeft, Root, Right
BRoot, Left, Right
CLeft, Right, Root
DRight, Left, Root
✗ Incorrect
Preorder traversal visits nodes in Root, then Left subtree, then Right subtree order.
Which traversal is best to use if you want to copy a tree exactly as it is?
AInorder
BPostorder
CLevel order
DPreorder
✗ Incorrect
Preorder traversal visits the root before children, making it ideal for copying the tree structure.
In Rust, how do you check if a tree node exists before visiting it in Preorder traversal?
ABoth B and C
Bif let Some(n) = node {}
Cmatch node { None => {}, _ => {} }
Dif node.is_none() {}
✗ Incorrect
Both 'if let Some(n) = node' and 'match node' can be used to check node existence in Rust.
What will be printed first in Preorder traversal of a tree with root value 10?
ARight child value
BLeft child value
C10
DNothing
✗ Incorrect
Preorder traversal prints the root node value first.
Explain the steps of Preorder traversal on a binary tree.
Think about the order Root, Left, Right.
You got /3 concepts.
Write a simple Rust function to perform Preorder traversal on a binary tree and print node values.
Use Option and recursion in Rust.
You got /4 concepts.
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, Root, Left
Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.
Step 2: Match the order with options
Root, Left, Right matches the order Root, Left, Right exactly.
Final Answer:
Root, Left, Right -> Option A
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
Swapping left and right subtree order
Thinking traversal starts with left child
2. Which Rust code snippet correctly implements preorder traversal for a binary tree node Node with left and right children?
easy
A. fn preorder(node: &Option) { if let Some(n) = node { print!("{} ", n.value); preorder(&n.left); preorder(&n.right); } }
B. fn preorder(node: &Option) { if let Some(n) = node { preorder(&n.left); print!("{} ", n.value); preorder(&n.right); } }
C. fn preorder(node: &Option) { if let Some(n) = node { preorder(&n.left); preorder(&n.right); print!("{} ", n.value); } }
D. fn preorder(node: &Option) { if let Some(n) = node { preorder(&n.right); print!("{} ", n.value); preorder(&n.left); } }
Solution
Step 1: Identify preorder traversal steps in code
Preorder visits root first (print value), then left subtree, then right subtree.
Step 2: Check each option's order
fn preorder(node: &Option) { if let Some(n) = node { print!("{} ", n.value); preorder(&n.left); preorder(&n.right); } } prints root value first, then recursively visits left and right children, matching preorder.
Final Answer:
Print root value first, then left, then right -> Option A
Quick Check:
Print root before children = preorder [OK]
Hint: Print root value before recursive calls for preorder [OK]
Common Mistakes:
Printing after left subtree (inorder)
Printing after both subtrees (postorder)
Swapping left and right recursive calls
3. Given the binary tree below, what is the preorder traversal output?
10
/ \
5 15
/ / \
3 12 20
medium
A. [10, 15, 20, 12, 5, 3]
B. [3, 5, 10, 12, 15, 20]
C. [5, 3, 10, 12, 15, 20]
D. [10, 5, 3, 15, 12, 20]
Solution
Step 1: Start at root and visit nodes in preorder
Visit root 10, then left subtree: 5, then 3. Then right subtree: 15, then 12, then 20.