0
0
DSA Javascriptprogramming~20 mins

Boundary Traversal of Binary Tree in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boundary Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Boundary Traversal on a Simple Binary Tree
What is the output of the boundary traversal for the given binary tree?
DSA Javascript
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));
A[1, 2, 4, 5, 6, 7, 3]
B[1, 2, 4, 5, 7, 6, 3]
C[1, 2, 4, 5, 6, 3, 7]
D[1, 4, 5, 6, 7, 2, 3]
Attempts:
2 left
💡 Hint
Remember to include left boundary (excluding leaves), all leaves, then right boundary in reverse order.
Predict Output
intermediate
2:00remaining
Boundary Traversal Output for Left Skewed Tree
What will be the output of the boundary traversal for this left skewed binary tree?
DSA Javascript
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));
A[10, 40, 30, 20]
B[10, 20, 30]
C[40, 30, 20, 10]
D[10, 20, 30, 40]
Attempts:
2 left
💡 Hint
In a left skewed tree, left boundary and leaves overlap.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Boundary Traversal Code
The following code attempts to perform boundary traversal but produces incorrect output. What is the bug?
DSA Javascript
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]);
  }
}
AThe function should start from node.left instead of node.right.
BThe traversal of right boundary should prioritize right child first, not left child.
CThe temp array should be reversed before pushing to result, but it is not reversed.
DThe function should add leaves inside the loop, not outside.
Attempts:
2 left
💡 Hint
Check the order of child traversal in right boundary collection.
🧠 Conceptual
advanced
1:30remaining
Understanding Leaf Node Identification in Boundary Traversal
In boundary traversal, why is it important to check if a node is a leaf before adding it to the left or right boundary?
ATo avoid adding leaf nodes twice since leaves are added separately in the traversal.
BBecause leaf nodes do not belong to the boundary of the tree.
CTo ensure leaf nodes are only added in the right boundary traversal.
DTo skip leaf nodes entirely from the traversal.
Attempts:
2 left
💡 Hint
Think about how leaves are handled separately in the traversal.
Predict Output
expert
3:00remaining
Boundary Traversal Output for Complex Tree with Missing Children
What is the output of the boundary traversal for the following binary tree?
DSA Javascript
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));
A[1, 2, 4, 5, 6, 7, 3, 8]
B[1, 2, 5, 4, 6, 8, 7, 3]
C[1, 2, 4, 5, 6, 8, 7, 3]
D[1, 2, 4, 5, 6, 7, 8, 3]
Attempts:
2 left
💡 Hint
Remember to add left boundary excluding leaves, then all leaves, then right boundary in reverse order.