Challenge - 5 Problems
Boundary Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Boundary Traversal on a Simple Binary Tree
What is the output of the boundary traversal for the given binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def printBoundary(root): res = [] def leftBoundary(node): if node and (node.left or node.right): res.append(node.val) if node.left: leftBoundary(node.left) else: leftBoundary(node.right) def leaves(node): if node: leaves(node.left) if not node.left and not node.right: res.append(node.val) leaves(node.right) def rightBoundary(node): if node and (node.left or node.right): if node.right: rightBoundary(node.right) else: rightBoundary(node.left) res.append(node.val) if root: res.append(root.val) leftBoundary(root.left) leaves(root.left) leaves(root.right) rightBoundary(root.right) return res root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) output = printBoundary(root) print(output)
Attempts:
2 left
💡 Hint
Remember the boundary traversal order: root, left boundary (excluding leaves), leaves, right boundary (excluding leaves, in reverse).
✗ Incorrect
The boundary traversal starts with the root (1), then left boundary nodes (2), then all leaves from left to right (4, 5, 6, 7), and finally the right boundary nodes in reverse order (3).
🧠 Conceptual
intermediateUnderstanding Boundary Traversal Components
Which of the following correctly describes the order of nodes visited in a boundary traversal of a binary tree?
Attempts:
2 left
💡 Hint
Think about the traversal starting from the root and how boundaries are defined.
✗ Incorrect
Boundary traversal visits the root first, then the left boundary excluding leaves, then all leaves from left to right, and finally the right boundary excluding leaves in reverse order.
🔧 Debug
advancedIdentify the Bug in Boundary Traversal Code
Given the following code snippet for boundary traversal, what error will it produce when run on a binary tree with only a root node?
DSA Python
def printBoundary(root): res = [] def leftBoundary(node): if node and (node.left or node.right): res.append(node.val) if node.left: leftBoundary(node.left) else: leftBoundary(node.right) def leaves(node): if node: leaves(node.left) if not node.left and not node.right: res.append(node.val) leaves(node.right) def rightBoundary(node): if node and (node.left or node.right): if node.right: rightBoundary(node.right) else: rightBoundary(node.left) res.append(node.val) res.append(root.val) leftBoundary(root.left) leaves(root.left) leaves(root.right) rightBoundary(root.right) return res root = Node(10) print(printBoundary(root))
Attempts:
2 left
💡 Hint
Check how the functions handle None nodes and if recursion stops correctly.
✗ Incorrect
The code correctly handles None nodes by checking if node is None before recursing, so it returns [10] without errors for a single root node.
🚀 Application
advancedBoundary Traversal Output for an Unbalanced Tree
What is the boundary traversal output for the following unbalanced binary tree?
Structure:
- Root: 10
- Root.left: 20
- Root.left.left: 40
- Root.left.left.right: 50
- Root.right: 30
- Root.right.right: 60
- Root.right.right.left: 70
Attempts:
2 left
💡 Hint
Remember to include left boundary excluding leaves, all leaves, and right boundary excluding leaves in reverse.
✗ Incorrect
Left boundary excluding leaves: 20, 40 (40 has a right child so it's boundary). Leaves: 50, 70. Right boundary excluding leaves in reverse: 60, 30. Root is 10 at start.
🧠 Conceptual
expertComplexity Analysis of Boundary Traversal
What is the time complexity of performing a boundary traversal on a binary tree with n nodes?
Attempts:
2 left
💡 Hint
Consider how many times each node is processed in the traversal.
✗ Incorrect
Each node is visited once either as part of left boundary, leaves, or right boundary, so total time is linear in number of nodes.
