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 Tree
What is the output of the boundary traversal for the following binary tree?
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
Boundary traversal order is: root, left boundary (excluding leaves), leaves (left to right), right boundary (excluding leaves, bottom to top).
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
Boundary traversal order is: root, left boundary (excluding leaves), leaves (left to right), right boundary (excluding leaves, bottom to top).
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BoundaryTraversal { public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.left.right.left = new Node(7); root.left.right.right = new Node(8); root.right.right = new Node(6); List<Integer> result = boundaryTraversal(root); System.out.println(result); } static List<Integer> boundaryTraversal(Node root) { List<Integer> res = new ArrayList<>(); if (root == null) return res; if (!isLeaf(root)) res.add(root.data); addLeftBoundary(root.left, res); addLeaves(root, res); addRightBoundary(root.right, res); return res; } static boolean isLeaf(Node node) { return node.left == null && node.right == null; } static void addLeftBoundary(Node node, List<Integer> res) { Node cur = node; while (cur != null) { if (!isLeaf(cur)) res.add(cur.data); if (cur.left != null) cur = cur.left; else cur = cur.right; } } static void addRightBoundary(Node node, List<Integer> res) { Stack<Integer> stack = new Stack<>(); Node cur = node; while (cur != null) { if (!isLeaf(cur)) stack.push(cur.data); if (cur.right != null) cur = cur.right; else cur = cur.left; } while (!stack.isEmpty()) res.add(stack.pop()); } static void addLeaves(Node node, List<Integer> res) { if (node == null) return; if (isLeaf(node)) { res.add(node.data); return; } addLeaves(node.left, res); addLeaves(node.right, res); } }
Attempts:
2 left
💡 Hint
Remember to include root, left boundary excluding leaves, all leaves left to right, then right boundary excluding leaves in reverse.
✗ Incorrect
The boundary traversal starts with root (1). Left boundary excluding leaves is 2. Leaves from left to right are 4, 7, 8, 6. Right boundary excluding leaves is 3 (added bottom to top). So final order is [1, 2, 4, 7, 8, 6, 3].
🧠 Conceptual
intermediateUnderstanding Boundary Traversal Components
Which of the following statements about boundary traversal of a binary tree is TRUE?
Attempts:
2 left
💡 Hint
Think about the order and inclusion rules for each part of the boundary.
✗ Incorrect
Left boundary excludes leaf nodes to avoid duplicates with leaves. Leaves are added between left and right boundaries. Root is always added if not leaf. Right boundary is added bottom to top excluding leaves.
🔧 Debug
advancedIdentify the Bug in Boundary Traversal Code
What error or issue will occur when running this boundary traversal code snippet on a binary tree with only root node?
Code snippet:
Code snippet:
if (!isLeaf(root)) res.add(root.data); addLeftBoundary(root.left, res); addLeaves(root, res); addRightBoundary(root.right, res);
Attempts:
2 left
💡 Hint
Check the condition for adding root node when it is a leaf.
✗ Incorrect
If the root is a leaf node, the condition (!isLeaf(root)) fails and root is not added. Since root is the only node, it will be missing from the result.
❓ Predict Output
advancedOutput of Boundary Traversal on a Skewed Tree
What is the output of the boundary traversal for this right skewed binary tree?
Tree structure:
1 -> 2 -> 3 -> 4 (each node has only right child)
Use the standard boundary traversal rules.
Tree structure:
1 -> 2 -> 3 -> 4 (each node has only right child)
Use the standard boundary traversal rules.
DSA Java
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BoundaryTraversal { public static void main(String[] args) { Node root = new Node(1); root.right = new Node(2); root.right.right = new Node(3); root.right.right.right = new Node(4); List<Integer> result = boundaryTraversal(root); System.out.println(result); } static List<Integer> boundaryTraversal(Node root) { List<Integer> res = new ArrayList<>(); if (root == null) return res; if (!isLeaf(root)) res.add(root.data); addLeftBoundary(root.left, res); addLeaves(root, res); addRightBoundary(root.right, res); return res; } static boolean isLeaf(Node node) { return node.left == null && node.right == null; } static void addLeftBoundary(Node node, List<Integer> res) { Node cur = node; while (cur != null) { if (!isLeaf(cur)) res.add(cur.data); if (cur.left != null) cur = cur.left; else cur = cur.right; } } static void addRightBoundary(Node node, List<Integer> res) { Stack<Integer> stack = new Stack<>(); Node cur = node; while (cur != null) { if (!isLeaf(cur)) stack.push(cur.data); if (cur.right != null) cur = cur.right; else cur = cur.left; } while (!stack.isEmpty()) res.add(stack.pop()); } static void addLeaves(Node node, List<Integer> res) { if (node == null) return; if (isLeaf(node)) { res.add(node.data); return; } addLeaves(node.left, res); addLeaves(node.right, res); } }
Attempts:
2 left
💡 Hint
Right boundary is added bottom to top excluding leaves; leaves are added separately.
✗ Incorrect
Root is 1. Left boundary is empty (no left child). Leaves are node 4. Right boundary excluding leaves is 2 and 3 added bottom to top, so order is 1 (root), leaves (4), right boundary reversed (3, 2).
🚀 Application
expertCount Nodes in Boundary Traversal
Given a binary tree, how many nodes will be in the boundary traversal output for the tree below?
Tree structure:
10
/ \
20 30
/ / \
40 50 60
\
70
Use standard boundary traversal rules.
Tree structure:
10
/ \
20 30
/ / \
40 50 60
\
70
Use standard boundary traversal rules.
Attempts:
2 left
💡 Hint
Count root, left boundary excluding leaves, leaves, and right boundary excluding leaves carefully without duplicates.
✗ Incorrect
Root: 10 (1 node). Left boundary excluding leaves: 20 (40 is leaf). Leaves: 40, 70, 60 (3 nodes). Right boundary excluding leaves: 30, 50 (50 is not leaf because it has right child 70). Total nodes = 1 + 1 + 3 + 2 = 7.
