0
0
DSA Javascriptprogramming~10 mins

Boundary Traversal of Binary Tree in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a node is a leaf node.

DSA Javascript
function isLeaf(node) {
  return node.left === null && node.[1] === null;
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cvalue
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one child instead of both.
Using wrong property names like 'value' or 'parent'.
2fill in blank
medium

Complete the code to add the left boundary nodes (excluding leaves) to the result array.

DSA Javascript
function addLeftBoundary(node, result) {
  let curr = node.left;
  while (curr !== null) {
    if (!isLeaf(curr)) result.push(curr.value);
    curr = curr.[1];
  }
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Moving down right child instead of left.
Adding leaf nodes to the boundary.
3fill in blank
hard

Fix the error in the function that adds leaf nodes to the result array.

DSA Javascript
function addLeaves(node, result) {
  if (node === null) return;
  if (isLeaf(node)) {
    result.push(node.[1]);
    return;
  }
  addLeaves(node.left, result);
  addLeaves(node.right, result);
}
Drag options to blanks, or click blank then click option'
Aparent
Bleft
Cvalue
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing the left or right child instead of the node's value.
Using undefined properties like 'parent'.
4fill in blank
hard

Fill both blanks to add the right boundary nodes (excluding leaves) in reverse order.

DSA Javascript
function addRightBoundary(node, result) {
  let curr = node.[1];
  const stack = [];
  while (curr !== null) {
    if (!isLeaf(curr)) stack.push(curr.value);
    curr = curr.[2];
  }
  while (stack.length > 0) {
    result.push(stack.pop());
  }
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using left child to start the right boundary.
Not reversing the collected nodes before adding.
5fill in blank
hard

Fill the blank to complete the boundary traversal function.

DSA Javascript
function boundaryTraversal(root) {
  if (root === null) return [];
  const result = [root.[1]];
  addLeftBoundary(root, result);
  addLeaves(root.left, result);
  addLeaves(root.right, result);
  addRightBoundary(root, result);
  return result;
}
Drag options to blanks, or click blank then click option'
Aslice
Bpush
Cpop
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Adding leaves from root instead of its subtrees.
Wrong order of adding boundaries and leaves.
Forgetting to handle null root.