📖 Scenario: Imagine you have a family tree and you want to visit each family member in a special order: first the left child, then the parent, and finally 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, then print the values.
📋 What You'll Learn
Create a binary tree with nodes having integer values
Add a helper variable to hold the traversal result
Write a recursive method to perform inorder traversal (left, root, right)
Print the traversal result as a list of node values
💡 Why This Matters
🌍 Real World
Tree traversal is used in many applications like searching files in folders, organizing data, and parsing expressions.
💼 Career
Understanding tree traversal is important for software developers working with data structures, databases, and algorithms.
Progress0 / 4 steps
1
Create the binary tree nodes
Create a class called Node with an integer Value and two Node references called Left and Right. Then create a root node with value 1, set its left child to a node with value 2, and its right child to a node with value 3.
DSA C#
Hint
Define the Node class with a constructor. Then create root and assign its Left and Right properties.
2
Create a list to hold traversal results
Create a List<int> called result to store the values of nodes visited during inorder traversal.
DSA C#
Hint
Use List<int> from System.Collections.Generic to store values.
3
Write the inorder traversal method
Write a recursive method called InorderTraversal that takes a Node parameter called node. It should visit the left child first by calling itself with node.Left, then add node.Value to the result list, then visit the right child by calling itself with node.Right. Make sure to check if node is not null before visiting.
DSA C#
Hint
Use recursion to visit left child, then current node, then right child. Add values to result.
4
Print the inorder traversal result
Print the result list as a comma-separated string using string.Join.
DSA C#
Hint
Use Console.WriteLine(string.Join(",", result)) to print the values separated by commas.
Practice
(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
easy
A. Left subtree, Root, Right subtree
B. Root, Left subtree, Right subtree
C. Right subtree, Root, Left subtree
D. Root, Right subtree, Left subtree
Solution
Step 1: Understand inorder traversal definition
Inorder traversal means visiting the left subtree first, then the root node, and finally the right subtree.
Step 2: Apply the order to the tree nodes
This order ensures nodes are visited in ascending order for binary search trees.
Final Answer:
Left subtree, Root, Right subtree -> Option A
Quick Check:
Inorder = Left, Root, Right [OK]
Hint: Remember: Inorder = Left, Root, Right [OK]
Common Mistakes:
Confusing inorder with preorder or postorder
Visiting root before left subtree
Visiting right subtree before root
2. Which of the following C# method signatures correctly represents an inorder traversal method for a binary tree node class named Node?
easy
A. void InorderTraversal(Node root)
B. int InorderTraversal(Node root)
C. Node InorderTraversal()
D. bool InorderTraversal(Node root)
Solution
Step 1: Identify the purpose of the method
An inorder traversal method typically visits nodes and prints or processes them without returning a value, so it should be void.
Step 2: Check method signature options
void InorderTraversal(Node root) uses void return type and accepts a Node parameter, which is correct for traversal.
Final Answer:
void InorderTraversal(Node root) -> Option A
Quick Check:
Traversal methods usually return void [OK]
Hint: Traversal methods usually return void [OK]
Common Mistakes:
Using return types like int or bool unnecessarily
Missing the root parameter
Returning Node instead of void
3. Given the following binary tree:
2
/ \
1 3
What is the output of the inorder traversal method that prints node values?
medium
A. 1 3 2
B. 2 1 3
C. 3 2 1
D. 1 2 3
Solution
Step 1: Apply inorder traversal on the tree
Visit left subtree (1), then root (2), then right subtree (3).
Step 2: Write the visited nodes in order
The nodes are visited in order: 1, 2, 3.
Final Answer:
1 2 3 -> Option D
Quick Check:
Inorder traversal output = 1 2 3 [OK]
Hint: Inorder prints nodes in ascending order for BST [OK]
C. It visits right subtree before left subtree, reversing inorder order
D. It does not print the root value
Solution
Step 1: Analyze the traversal order in the code
The code visits root.Right first, then root, then root.Left, which is reverse of inorder.
Step 2: Identify the correct inorder order
Inorder should visit left subtree first, then root, then right subtree.
Final Answer:
It visits right subtree before left subtree, reversing inorder order -> Option C
Quick Check:
Inorder = Left, Root, Right [OK]
Hint: Inorder must visit left subtree before right [OK]
Common Mistakes:
Swapping left and right subtree calls
Forgetting to check for null root
Not printing root value
5. You have a binary tree where each node contains an integer. You want to collect all node values in a list using inorder traversal. Which C# code snippet correctly implements this?
hard
A. void Inorder(Node root, List<int> result) {
if (root == null) return;
result.Add(root.Value);
Inorder(root.Left, result);
Inorder(root.Right, result);
}
B. void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Left, result);
result.Add(root.Value);
Inorder(root.Right, result);
}
C. void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Right, result);
result.Add(root.Value);
Inorder(root.Left, result);
}
D. void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Left, result);
Inorder(root.Right, result);
result.Add(root.Value);
}
Solution
Step 1: Recall inorder traversal order
Inorder visits left subtree, then root, then right subtree.
Step 2: Check which code matches this order
void Inorder(Node root, List<int> result) {
if (root == null) return;
Inorder(root.Left, result);
result.Add(root.Value);
Inorder(root.Right, result);
} visits left subtree, adds root value, then visits right subtree, correctly collecting values in order.
Final Answer:
void Inorder(Node root, List<int> result) { if (root == null) return; Inorder(root.Left, result); result.Add(root.Value); Inorder(root.Right, result); } -> Option B
Quick Check:
Left, Root, Right order with result.Add(root.Value) in middle [OK]
Hint: Add root value between left and right recursive calls [OK]
Common Mistakes:
Adding root value before left subtree
Adding root value after right subtree
Swapping left and right subtree calls
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.