📖 Scenario: You are working with a simple family tree where each person can have up to two children. You want to visit each person starting from the oldest ancestor, then their left child, then their right child.
🎯 Goal: Build a program that creates a tree structure and prints the names of people in preorder traversal order: root first, then left child, then right child.
📋 What You'll Learn
Create a tree node class with name, left, and right properties
Create a tree with exactly three nodes: "Grandparent", "Parent", and "Child"
Write a preorder traversal function called preorderTraversal that visits nodes in root-left-right order
Print the names of the nodes in preorder traversal separated by spaces
💡 Why This Matters
🌍 Real World
Tree traversal is used in many real-world applications like file system navigation, organization charts, and parsing expressions.
💼 Career
Understanding tree traversal is essential for software developers working with hierarchical data, databases, and algorithms.
Progress0 / 4 steps
1
Create TreeNode class and build the tree
Create a class called TreeNode with a constructor that takes a name string and sets left and right to null. Then create three nodes: grandparent with name "Grandparent", parent with name "Parent", and child with name "Child". Connect grandparent.left to parent and parent.left to child.
DSA Typescript
Hint
Define the TreeNode class with name, left, and right. Then create the three nodes and link them as described.
2
Create a helper array to store traversal result
Create an empty array called result of type string[] to store the names of nodes during traversal.
DSA Typescript
Hint
Just create an empty array named result to hold the names as strings.
3
Write preorder traversal function
Write a function called preorderTraversal that takes a node of type TreeNode | null. If node is not null, push node.name to result, then recursively call preorderTraversal on node.left and node.right.
DSA Typescript
Hint
Use a function that checks if the node is not null, then adds the name to result, then calls itself on left and right children.
4
Run traversal and print result
Call preorderTraversal with grandparent as argument. Then print the result array joined by spaces using console.log(result.join(" ")).
DSA Typescript
Hint
Call the traversal starting from grandparent and print the names joined by spaces.
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, Left, Root
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: Match the order with options
The order Root, Left, Right matches the preorder traversal definition exactly.
Final Answer:
Root, Left, Right -> Option A
Quick Check:
Preorder = Root, Left, Right [OK]
Hint: Preorder always starts with the root node first [OK]
Common Mistakes:
Confusing preorder with inorder or postorder
Visiting left or right before root
Mixing up traversal orders
2. Which of the following TypeScript function signatures correctly represents a preorder traversal function for a binary tree node?
easy
A. function preorder(node: TreeNode): number[]
B. function preorder(node: TreeNode | null): void
C. function preorder(): TreeNode
D. function preorder(node: TreeNode, result: number[]): number
Solution
Step 1: Analyze function parameters and return type
A preorder traversal function typically takes a node (or null) and returns void if it prints or processes nodes directly.
Step 2: Check each option
function preorder(node: TreeNode | null): void accepts a node or null and returns void, which fits a traversal that processes nodes without returning a value.
Final Answer:
function preorder(node: TreeNode | null): void -> Option B
Quick Check:
Traversal function signature with node or null and void return [OK]
Hint: Traversal functions usually take a node and return void or array [OK]
Common Mistakes:
Missing null in node parameter type
Wrong return type for traversal
Incorrect number of parameters
3. Given the following binary tree:
1
/ \
2 3
/ \
4 5
What is the output of a preorder traversal (Root, Left, Right)?
medium
A. [4, 2, 5, 1, 3]
B. [2, 4, 5, 1, 3]
C. [1, 2, 4, 5, 3]
D. [1, 3, 2, 4, 5]
Solution
Step 1: Start at root node 1
Preorder visits root first: 1
Step 2: Traverse left subtree of 1
Visit node 2, then its left child 4, then right child 5: 2, 4, 5