📖 Scenario: Imagine you have a family tree, and you want to visit each family member starting from the oldest ancestor, then moving to their children from left to right.
🎯 Goal: You will build a simple tree structure 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 class called Node with an integer Value and two children: Left and Right
Create a tree with exactly three nodes: root with value 1, left child with value 2, right child with value 3
Write a method called PreorderTraversal that visits nodes in preorder and collects their values in a list
Print the list of visited node values after traversal
💡 Why This Matters
🌍 Real World
Tree traversal is used in many areas like file system navigation, expression evaluation, and organizing data hierarchically.
💼 Career
Understanding tree traversal is important for software developers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Node class and the tree root
Create a class called Node with an integer property Value and two properties Left and Right of type Node. Then create a variable called root and assign it a new Node with Value 1.
DSA C#
Hint
Define the Node class with a constructor that sets the Value. Then create root as a new Node with value 1.
2
Add left and right children to the root
Add a left child to root with Value 2 and a right child with Value 3 by assigning new Node instances to root.Left and root.Right.
DSA C#
Hint
Use root.Left = new Node(2); and root.Right = new Node(3); to add children.
3
Write the PreorderTraversal method
Write a method called PreorderTraversal that takes a Node and a List<int> called result. It should add the current node's Value to result, then recursively call itself on the Left child, then on the Right child.
DSA C#
Hint
Check if node is null first. Then add node.Value to result. Then call PreorderTraversal on node.Left and node.Right.
4
Call PreorderTraversal and print the result
Create a List<int> called visited. Call PreorderTraversal with root and visited. Then print the values in visited separated by spaces using Console.WriteLine(string.Join(" ", visited)).
DSA C#
Hint
Create a list called visited, call PreorderTraversal(root, visited), then print with Console.WriteLine(string.Join(" ", visited)).
Practice
(1/5)
1. What is the order of nodes visited in a preorder tree traversal?
easy
A. Left, Root, Right
B. Right, Left, Root
C. Left, Right, Root
D. Root, Left, Right
Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits the root node first, then recursively visits the left subtree, followed by the right subtree.
Step 2: Confirm the visiting order
The visiting order is Root, then Left subtree, then Right subtree.
Final Answer:
Root, Left, Right -> Option D
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
Visiting left subtree before root
Visiting right subtree before left
2. Which of the following C# method signatures correctly represents a preorder traversal method for a binary tree node class named Node?