Consider the following binary tree structure and the inorder traversal function. What will be printed?
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn inorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
inorder(&node.left, result);
result.push(node.val);
inorder(&node.right, result);
}
}
fn main() {
let tree = Some(Box::new(Node {
val: 2,
left: Some(Box::new(Node {
val: 1,
left: None,
right: None,
})),
right: Some(Box::new(Node {
val: 3,
left: None,
right: None,
})),
}));
let mut result = Vec::new();
inorder(&tree, &mut result);
println!("{:?}", result);
}Inorder traversal visits left child, then root, then right child.
The inorder traversal visits nodes in the order: left subtree, root, right subtree. For this tree, it visits 1, then 2, then 3.
Given the following tree and inorder traversal function, what is the output?
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn inorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
inorder(&node.left, result);
result.push(node.val);
inorder(&node.right, result);
}
}
fn main() {
let tree = Some(Box::new(Node {
val: 5,
left: Some(Box::new(Node {
val: 3,
left: Some(Box::new(Node {
val: 2,
left: None,
right: None,
})),
right: None,
})),
right: Some(Box::new(Node {
val: 7,
left: None,
right: Some(Box::new(Node {
val: 8,
left: None,
right: None,
})),
})),
}));
let mut result = Vec::new();
inorder(&tree, &mut result);
println!("{:?}", result);
}Remember inorder visits left subtree, root, then right subtree recursively.
The traversal visits nodes in this order: 2 (leftmost), 3 (parent), 5 (root), 7 (right child), 8 (rightmost).
Examine the code below. It is intended to perform inorder traversal but causes a stack overflow error. What is the cause?
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn inorder(root: &Option<Box<Node>>, result: &mut Vec<i32>) {
if let Some(node) = root {
inorder(&node.left, result);
result.push(node.val);
inorder(&node.left, result); // mistake here
}
}
fn main() {
let tree = Some(Box::new(Node {
val: 1,
left: None,
right: None,
}));
let mut result = Vec::new();
inorder(&tree, &mut result);
println!("{:?}", result);
}Check the recursive calls carefully for repeated calls on the same child.
The code calls inorder(&node.left, result) twice and never calls inorder on node.right, causing infinite recursion on the left subtree.
In a binary search tree, what does the inorder traversal output represent?
Recall the BST property: left child < root < right child.
Inorder traversal visits nodes in ascending order for BST because it visits left subtree (smaller), root, then right subtree (larger).
Given the tree below, how many nodes will the inorder traversal visit?
Tree structure:
- Root: 10
- Left child: 5 (with left child 3 and right child 7)
- Right child: 15 (with right child 18)
Count all nodes in the tree; inorder visits all nodes exactly once.
The tree has nodes: 10, 5, 3, 7, 15, 18. Total 6 nodes, so the correct answer is 6.
