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 following binary tree?
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
DSA C#
class Node { public int data; public Node left, right; public Node(int d) { data = d; left = right = null; } } // Boundary traversal function returns list of node data in boundary order List<int> BoundaryTraversal(Node root) { List<int> res = new List<int>(); if (root == null) return res; res.Add(root.data); void AddLeftBoundary(Node node) { Node cur = node; while (cur != null) { if (cur.left != null || cur.right != null) res.Add(cur.data); if (cur.left != null) cur = cur.left; else cur = cur.right; } } void AddLeaves(Node node) { if (node == null) return; AddLeaves(node.left); if (node.left == null && node.right == null) res.Add(node.data); AddLeaves(node.right); } void AddRightBoundary(Node node) { Stack<int> s = new Stack<int>(); Node cur = node; while (cur != null) { if (cur.left != null || cur.right != null) s.Push(cur.data); if (cur.right != null) cur = cur.right; else cur = cur.left; } while (s.Count > 0) res.Add(s.Pop()); } AddLeftBoundary(root.left); AddLeaves(root.left); AddLeaves(root.right); AddRightBoundary(root.right); return res; }
Attempts:
2 left
💡 Hint
Remember boundary traversal includes left boundary (excluding leaves), all leaves, and right boundary (excluding leaves) in reverse.
✗ Incorrect
The boundary traversal starts at root (1), then left boundary excluding leaves (2), then leaves in left to right order (4,7,8,6), then right boundary excluding leaves in reverse (3). Node 5 is not on boundary as it is internal leaf's parent.
❓ Predict Output
intermediateBoundary Traversal Output for Single Node Tree
What is the output of the boundary traversal for a binary tree with only one node (value 10)?
DSA C#
class Node { public int data; public Node left, right; public Node(int d) { data = d; left = right = null; } } List<int> BoundaryTraversal(Node root) { List<int> res = new List<int>(); if (root == null) return res; res.Add(root.data); void AddLeftBoundary(Node node) { Node cur = node; while (cur != null) { if (cur.left != null || cur.right != null) res.Add(cur.data); if (cur.left != null) cur = cur.left; else cur = cur.right; } } void AddLeaves(Node node) { if (node == null) return; AddLeaves(node.left); if (node.left == null && node.right == null) res.Add(node.data); AddLeaves(node.right); } void AddRightBoundary(Node node) { Stack<int> s = new Stack<int>(); Node cur = node; while (cur != null) { if (cur.left != null || cur.right != null) s.Push(cur.data); if (cur.right != null) cur = cur.right; else cur = cur.left; } while (s.Count > 0) res.Add(s.Pop()); } AddLeftBoundary(root.left); AddLeaves(root.left); AddLeaves(root.right); AddRightBoundary(root.right); return res; }
Attempts:
2 left
💡 Hint
A single node tree's boundary traversal is just the root node.
✗ Incorrect
Since the tree has only one node, the boundary traversal is just that node's value.
🔧 Debug
advancedIdentify the Logical Error in Boundary Traversal Code
Given the following code snippet for boundary traversal, what is the logical error that causes incorrect output?
DSA C#
void AddLeftBoundary(Node node) {
Node cur = node;
while (cur != null) {
res.Add(cur.data);
if (cur.left != null) cur = cur.left;
else cur = cur.right;
}
}
void AddRightBoundary(Node node) {
Stack<int> s = new Stack<int>();
Node cur = node;
while (cur != null) {
s.Push(cur.data);
if (cur.right != null) cur = cur.right;
else cur = cur.left;
}
while (s.Count > 0) res.Add(s.Pop());
}Attempts:
2 left
💡 Hint
Check if leaves are excluded from left and right boundary to avoid duplicates.
✗ Incorrect
The code adds all nodes in left and right boundary including leaves, which are also added separately in leaves traversal, causing duplicates.
🧠 Conceptual
advancedUnderstanding Boundary Traversal Components
Which of the following correctly describes the three main parts of a boundary traversal of a binary tree?
Attempts:
2 left
💡 Hint
Boundary traversal excludes leaves from left and right boundaries to avoid duplicates.
✗ Incorrect
Boundary traversal includes left boundary excluding leaves, all leaves, and right boundary excluding leaves in reverse order.
🚀 Application
expertNumber of Nodes in Boundary Traversal Output
Given a perfect binary tree of height 3 (levels 0 to 3), how many nodes will be in the boundary traversal output?
Attempts:
2 left
💡 Hint
Count nodes on left boundary excluding leaves, leaves, and right boundary excluding leaves carefully.
✗ Incorrect
A perfect binary tree of height 3 has 15 nodes. Left boundary excluding leaves has 2 nodes, leaves are 8 nodes, right boundary excluding leaves has 2 nodes, plus root counted once. Total = 1 (root) + 2 + 8 + 2 = 13 but root counted once, so total nodes in boundary traversal is 9 (root + left boundary excluding leaves + leaves + right boundary excluding leaves without duplicates).
