0
0
DSA Typescriptprogramming~10 mins

Boundary Traversal of Binary Tree in DSA Typescript - 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 Typescript
function isLeaf(node: TreeNode | null): boolean {
  return node !== null && node.left === null && node.[1] === null;
}
Drag options to blanks, or click blank then click option'
Aright
Bparent
Cvalue
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only left child for null.
Using incorrect property names like 'value' or 'data'.
2fill in blank
medium

Complete the code to traverse the left boundary excluding leaf nodes.

DSA Typescript
function addLeftBoundary(root: TreeNode | null, res: number[]): void {
  let curr = root?.left;
  while (curr !== null) {
    if (!isLeaf(curr)) res.push(curr.val);
    curr = curr.left !== null ? curr.left : curr.[1];
  }
}
Drag options to blanks, or click blank then click option'
Anext
Bparent
Cright
Dcurr.val
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to parent or next node which is not part of boundary.
Using node value instead of node reference.
3fill in blank
hard

Fix the error in the code to add leaf nodes in left-to-right order.

DSA Typescript
function addLeaves(root: TreeNode | null, res: number[]): void {
  if (root === null) return;
  if (isLeaf(root)) {
    res.push(root.val);
    return;
  }
  addLeaves(root.[1], res);
  addLeaves(root.right, res);
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Traversing right subtree before left subtree.
Using incorrect property names.
4fill in blank
hard

Fill both blanks to add the right boundary nodes in bottom-up order excluding leaf nodes.

DSA Typescript
function addRightBoundary(root: TreeNode | null, res: number[]): void {
  let curr = root?.right;
  const stack: number[] = [];
  while (curr !== null) {
    if (!isLeaf(curr)) stack.push(curr.val);
    curr = curr.[1] !== null ? curr.[1] : curr.[2];
  }
  while (stack.length > 0) {
    res.push(stack.pop()!);
  }
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Going left first instead of right.
Not using a stack to reverse order.
5fill in blank
hard

Fill all three blanks to complete the boundary traversal function.

DSA Typescript
function boundaryTraversal(root: TreeNode | null): number[] {
  const res: number[] = [];
  if (root === null) return res;
  if (!isLeaf(root)) res.push(root.val);
  addLeftBoundary(root, res);
  addLeaves(root, res);
  addRightBoundary(root, res);
  return res;
}

// Helper function to check leaf
function isLeaf(node: TreeNode | null): boolean {
  return node !== null && node.left === null && node.[1] === null;
}

// Add left boundary
function addLeftBoundary(root: TreeNode | null, res: number[]): void {
  let curr = root?.left;
  while (curr !== null) {
    if (!isLeaf(curr)) res.push(curr.val);
    curr = curr.left !== null ? curr.left : curr.[2];
  }
}

// Add leaves
function addLeaves(root: TreeNode | null, res: number[]): void {
  if (root === null) return;
  if (isLeaf(root)) {
    res.push(root.val);
    return;
  }
  addLeaves(root.left, res);
  addLeaves(root.right, res);
}

// Add right boundary
function addRightBoundary(root: TreeNode | null, res: number[]): void {
  let curr = root?.right;
  const stack: number[] = [];
  while (curr !== null) {
    if (!isLeaf(curr)) stack.push(curr.val);
    curr = curr.right !== null ? curr.right : curr.[3];
  }
  while (stack.length > 0) {
    res.push(stack.pop()!);
  }
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing left and right child in traversal.
Incorrect leaf node check.