Challenge - 5 Problems
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediatePreorder Traversal Output of a Simple Binary Tree
What is the output of the preorder traversal (Root, Left, Right) of the following binary tree?
Tree structure:
1
/ \ 2 3
/
4
Tree structure:
1
/ \ 2 3
/
4
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def preorder(root): if root is None: return [] return [root.val] + preorder(root.left) + preorder(root.right) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) result = preorder(root) print(result)
Attempts:
2 left
💡 Hint
Remember preorder means visit root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in the order: root, left child, right child. So we visit 1, then go left to 2, then left to 4, then back up and right to 3.
❓ Predict Output
intermediatePreorder Traversal Output with Null Children
What is the preorder traversal output of this binary tree?
Tree structure:
5
/ \ 6 None
\ 7
Tree structure:
5
/ \ 6 None
\ 7
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def preorder(root): if root is None: return [] return [root.val] + preorder(root.left) + preorder(root.right) root = Node(5) root.left = Node(6) root.left.right = Node(7) result = preorder(root) print(result)
Attempts:
2 left
💡 Hint
Preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Start at root 5, then go left to 6, then 6's right child 7. No right child of 5 exists.
🔧 Debug
advancedIdentify the Error in Preorder Traversal Implementation
What error will this preorder traversal code produce when run on a non-empty tree?
DSA Python
def preorder(root): if root is None: return [] return preorder(root.left) + preorder(root.right) + [root.val] root = Node(1) root.left = Node(2) root.right = Node(3) print(preorder(root))
Attempts:
2 left
💡 Hint
Check the order of concatenation in the return statement.
✗ Incorrect
The code visits left, then right, then root, which is postorder traversal, not preorder. The output is [2, 3, 1].
❓ Predict Output
advancedPreorder Traversal Output of a Larger Tree
What is the preorder traversal output of this binary tree?
Tree structure:
10
/ \ > 5 15
/ \ \ > 3 7 18
Tree structure:
10
/ \ > 5 15
/ \ \ > 3 7 18
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def preorder(root): if root is None: return [] return [root.val] + preorder(root.left) + preorder(root.right) root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(3) root.left.right = Node(7) root.right.right = Node(18) result = preorder(root) print(result)
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
✗ Incorrect
Visit 10 first, then left subtree (5, 3, 7), then right subtree (15, 18).
🧠 Conceptual
expertNumber of Nodes Visited in Preorder Traversal
Given a binary tree with 7 nodes, how many nodes does the preorder traversal visit?
Attempts:
1 left
💡 Hint
Preorder traversal visits every node exactly once.
✗ Incorrect
Preorder traversal visits every node in the tree exactly once, so it visits all 7 nodes.
