class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function boundaryTraversal(root) { if (!root) return []; const result = []; function isLeaf(node) { return !node.left && !node.right; } function addLeftBoundary(node) { let curr = node.left; while (curr) { if (!isLeaf(curr)) result.push(curr.data); if (curr.left) curr = curr.left; else curr = curr.right; } } function addLeaves(node) { if (isLeaf(node)) { result.push(node.data); return; } if (node.left) addLeaves(node.left); if (node.right) addLeaves(node.right); } function addRightBoundary(node) { let curr = node.right; const temp = []; while (curr) { if (!isLeaf(curr)) temp.push(curr.data); if (curr.right) curr = curr.right; else curr = curr.left; } for (let i = temp.length - 1; i >= 0; i--) { result.push(temp[i]); } } if (!isLeaf(root)) result.push(root.data); addLeftBoundary(root); addLeaves(root); addRightBoundary(root); return result; } // Tree construction const 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.right.left = new Node(6); root.right.right = new Node(7); console.log(boundaryTraversal(root));
The boundary traversal includes the root (1), left boundary nodes excluding leaves (2), all leaf nodes in left to right order (4, 5, 6, 7), and right boundary nodes excluding leaves in reverse order (3).
class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function boundaryTraversal(root) { if (!root) return []; const result = []; function isLeaf(node) { return !node.left && !node.right; } function addLeftBoundary(node) { let curr = node.left; while (curr) { if (!isLeaf(curr)) result.push(curr.data); if (curr.left) curr = curr.left; else curr = curr.right; } } function addLeaves(node) { if (isLeaf(node)) { result.push(node.data); return; } if (node.left) addLeaves(node.left); if (node.right) addLeaves(node.right); } function addRightBoundary(node) { let curr = node.right; const temp = []; while (curr) { if (!isLeaf(curr)) temp.push(curr.data); if (curr.right) curr = curr.right; else curr = curr.left; } for (let i = temp.length - 1; i >= 0; i--) { result.push(temp[i]); } } if (!isLeaf(root)) result.push(root.data); addLeftBoundary(root); addLeaves(root); addRightBoundary(root); return result; } // Left skewed tree const root = new Node(10); root.left = new Node(20); root.left.left = new Node(30); root.left.left.left = new Node(40); console.log(boundaryTraversal(root));
The root (10) is included first. The left boundary nodes excluding leaves are 20 and 30. The leaf node is 40. No right boundary nodes exist. So the traversal is [10, 20, 30, 40].
function addRightBoundary(node) {
let curr = node.right;
const temp = [];
while (curr) {
if (!isLeaf(curr)) temp.push(curr.data);
if (curr.left) curr = curr.left;
else curr = curr.right;
}
for (let i = temp.length - 1; i >= 0; i--) {
result.push(temp[i]);
}
}Right boundary traversal should move down the right child first to get the correct boundary nodes. The code incorrectly moves down the left child first, causing wrong nodes to be collected.
Leaf nodes are added once during the leaf collection step. Checking if a node is leaf before adding it to left or right boundary prevents duplication.
class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function boundaryTraversal(root) { if (!root) return []; const result = []; function isLeaf(node) { return !node.left && !node.right; } function addLeftBoundary(node) { let curr = node.left; while (curr) { if (!isLeaf(curr)) result.push(curr.data); if (curr.left) curr = curr.left; else curr = curr.right; } } function addLeaves(node) { if (isLeaf(node)) { result.push(node.data); return; } if (node.left) addLeaves(node.left); if (node.right) addLeaves(node.right); } function addRightBoundary(node) { let curr = node.right; const temp = []; while (curr) { if (!isLeaf(curr)) temp.push(curr.data); if (curr.right) curr = curr.right; else curr = curr.left; } for (let i = temp.length - 1; i >= 0; i--) { result.push(temp[i]); } } if (!isLeaf(root)) result.push(root.data); addLeftBoundary(root); addLeaves(root); addRightBoundary(root); return result; } // Complex tree const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.right = new Node(4); root.left.right.left = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); root.right.right.left = new Node(8); console.log(boundaryTraversal(root));
The left boundary excluding leaves is [2, 4]. Leaves are [5, 6, 8]. Right boundary excluding leaves in reverse order is [7, 3]. Root is 1. So combined: [1, 2, 4, 5, 6, 8, 7, 3].