📖 Scenario: Imagine you have a family tree and you want to visit each family member starting from the oldest ancestor, then visiting their children from left to right.
🎯 Goal: You will build a simple binary tree and write code to visit each node in preorder: first the root, then the left child, then the right child.
📋 What You'll Learn
Create a binary tree node struct with integer values
Build a small tree with exactly 3 nodes: root, left child, right child
Write a preorder traversal function that visits nodes in Root-Left-Right order
Print the values of nodes in preorder traversal
💡 Why This Matters
🌍 Real World
Tree traversal is used in many real-world tasks like searching file systems, organizing data, and parsing expressions.
💼 Career
Understanding tree traversal is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the binary tree nodes
Define a struct called TreeNode with fields val: i32, left: Option>, and right: Option>. Then create a variable root that is a TreeNode with val 1 and both left and right set to None.
DSA Rust
Hint
Use Option> for left and right to allow empty children.
2
Add left and right children to the root
Update the root variable to have a left child with val 2 and a right child with val 3. Use Some(Box::new(TreeNode { ... })) to create the children with left and right set to None.
DSA Rust
Hint
Use Some(Box::new(TreeNode { val: ..., left: None, right: None })) for each child.
3
Write the preorder traversal function
Write a function called preorder that takes a reference to an Option> called node. If node is Some, print the val, then recursively call preorder on the left child, then on the right child.
DSA Rust
Hint
Use if let Some(n) = node to check if node exists, then print and recurse.
4
Call preorder and print the traversal
In main, call preorder with a reference to Some(Box::new(root)). Then print a newline after traversal.
DSA Rust
Hint
Wrap root in Some(Box::new(...)) when calling preorder.
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.