📖 Scenario: Imagine you have a family tree stored in a computer program. You want to visit each family member in a special order: first the left child, then the parent, then the right child. This is called inorder traversal.
🎯 Goal: You will build a simple binary tree and write code to visit its nodes in inorder (left, root, right) order, printing each node's value.
📋 What You'll Learn
Create a binary tree node struct with integer values
Build a small binary tree with 3 nodes: root 2, left child 1, right child 3
Write a recursive function called inorder_traversal that visits nodes in left-root-right order
Print the node values in inorder traversal separated by spaces
💡 Why This Matters
🌍 Real World
Tree traversal is used in many applications like searching family trees, organizing files, and parsing expressions.
💼 Career
Understanding tree traversal is important for software engineers working with data structures, databases, and compilers.
Progress0 / 4 steps
1
Create the binary tree nodes
Create a struct called TreeNode with fields val: i32, left: Option>, and right: Option>. Then create a variable called root that is a TreeNode with value 2, left child with value 1, and right child with value 3.
DSA Rust
Hint
Use Option> for left and right to allow empty children.
2
Add the inorder traversal function
Add a recursive function called inorder_traversal that takes a reference to an Option> and prints the node values in inorder (left, root, right) order separated by spaces.
DSA Rust
Hint
Use if let Some(n) = node to check if the node exists before visiting children.
3
Call inorder traversal on the root
Inside main, call the inorder_traversal function with a reference to the root wrapped in Some(Box::new(root)).
DSA Rust
Hint
Wrap root in Some(Box::new(...)) to match the function parameter type.
4
Print the final inorder traversal output
Add a println!() after the inorder_traversal call in main to print a newline so the output looks clean.
DSA Rust
Hint
Use println!() to add a newline after printing all node values.
Practice
(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
easy
A. Left subtree, Root node, Right subtree
B. Root node, Left subtree, Right subtree
C. Right subtree, Root node, Left subtree
D. Root node, Right subtree, Left subtree
Solution
Step 1: Understand inorder traversal definition
Inorder traversal visits nodes in the order: left subtree first, then the root node, and finally the right subtree.
Step 2: Match the order with options
Left subtree, Root node, Right subtree matches the left-root-right order exactly.
Final Answer:
Left subtree, Root node, Right subtree -> Option A
Quick Check:
Inorder = Left, Root, Right [OK]
Hint: Remember: Inorder means Left, Root, then Right [OK]
Common Mistakes:
Confusing inorder with preorder or postorder
Mixing up the order of root and subtrees
Thinking inorder visits root first
2. Which Rust code snippet correctly implements the inorder traversal function signature for a binary tree node?
D. fn inorder(node: Option<TreeNode>) { /* traversal code */ }
Solution
Step 1: Understand Rust ownership and references for trees
Binary trees in Rust often use Option to allow null and heap allocation. Passing by reference (&Option) avoids ownership transfer during traversal.
Step 2: Check each option's signature
fn inorder(node: &Option) { /* traversal code */ } uses a reference to Option, which is correct for traversal without ownership moves. Others either take ownership or incorrect types.