Preorder traversal visits nodes in this order: Root first, then Left subtree, and finally Right subtree. It means you process the current node before its children.
Click to reveal answer
beginner
In preorder traversal, which node is visited first?
The root node is visited first in preorder traversal before visiting any child nodes.
Click to reveal answer
beginner
What is the order of visiting nodes in preorder traversal?
The order is: Root → Left subtree → Right subtree.
Click to reveal answer
intermediate
Why is preorder traversal useful?
Preorder traversal is useful to copy a tree or to get prefix expression of an expression tree because it processes the root before its children.
In preorder traversal, which node is visited immediately after the root?
AParent node
BRight child
CLeft child
DLeaf node
✗ Incorrect
Preorder visits root first, then the left child (left subtree).
What is the correct order of nodes visited in preorder traversal?
ALeft → Root → Right
BRight → Left → Root
CLeft → Right → Root
DRoot → Left → Right
✗ Incorrect
Preorder traversal visits root first, then left subtree, then right subtree.
Which traversal visits the root node before its children?
AInorder
BPreorder
CPostorder
DLevel order
✗ Incorrect
Preorder traversal visits the root node first.
If a tree has only one node, what is the preorder traversal output?
AThe single node
BEmpty
CNull
DDepends on children
✗ Incorrect
Preorder visits root first, so the single node is visited.
Which of these is NOT a use case of preorder traversal?
ASorting nodes in ascending order
BPrefix expression of expression tree
CCopying a tree
DVisiting root before children
✗ Incorrect
Sorting nodes in ascending order is usually done by inorder traversal in binary search trees.
Explain how preorder traversal works and the order in which nodes are visited.
Think about visiting the root before going down to children.
You got /4 concepts.
Describe a real-life example where preorder traversal would be useful.
Consider when you need to process a parent before its children.
You got /4 concepts.
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