Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

Choose your learning style10 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 Typescript
class Node {
  constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {}
}

function boundaryTraversal(root: Node | null): number[] {
  if (!root) return [];

  const result: number[] = [];

  function isLeaf(node: Node): boolean {
    return !node.left && !node.right;
  }

  function addLeftBoundary(node: Node | null) {
    let curr = node;
    while (curr) {
      if (!isLeaf(curr)) result.push(curr.data);
      if (curr.left) curr = curr.left;
      else curr = curr.right;
    }
  }

  function addLeaves(node: Node | null) {
    if (!node) return;
    if (isLeaf(node)) {
      result.push(node.data);
      return;
    }
    addLeaves(node.left);
    addLeaves(node.right);
  }

  function addRightBoundary(node: Node | null) {
    const temp: number[] = [];
    let curr = node;
    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.left);
  addLeaves(root);
  addRightBoundary(root.right);

  return result;
}

// Tree structure:
//       1
//      / \
//     2   3
//    / \   \
//   4   5   6
//      / \  /
//     7  8 9

const root = new Node(1,
  new Node(2,
    new Node(4),
    new Node(5,
      new Node(7),
      new Node(8)
    )
  ),
  new Node(3,
    null,
    new Node(6,
      new Node(9),
      null
    )
  )
);

console.log(boundaryTraversal(root));
A[1, 2, 4, 7, 8, 9, 6]
B[1, 2, 4, 7, 8, 5, 9, 6, 3]
C[1, 2, 4, 7, 8, 9, 6, 3]
D[1, 4, 7, 8, 9, 6, 3]
Attempts:
2 left
💡 Hint
Remember to include left boundary (excluding leaves), all leaves, and right boundary (excluding leaves) in reverse order.
🧠 Conceptual
intermediate
1:30remaining
Understanding Boundary Traversal Components
Which of the following correctly describes the components included in the boundary traversal of a binary tree?
ARoot node, all nodes in left subtree, all leaf nodes, all nodes in right subtree
BRoot node and all nodes at the deepest level
COnly leaf nodes from left to right
DRoot node, left boundary excluding leaves, all leaf nodes, right boundary excluding leaves in reverse order
Attempts:
2 left
💡 Hint
Think about which nodes form the outer edge of the tree.
🔧 Debug
advanced
1:30remaining
Identify the Error in Boundary Traversal Code
Given the following code snippet for boundary traversal, what error will it produce when run on a non-empty tree?
DSA Typescript
function addLeftBoundary(node) {
  let curr = node;
  while (curr) {
    if (!isLeaf(curr)) result.push(curr.data);
    if (curr.left) curr = curr.left;
    else curr = curr.right;
  }
}

// Assume isLeaf and result are defined globally

addLeftBoundary(null);
ANo error, function completes silently
BTypeError: Cannot read property 'left' of null
CReferenceError: isLeaf is not defined
DInfinite loop
Attempts:
2 left
💡 Hint
Consider what happens when the input node is null.
❓ Predict Output
advanced
2:00remaining
Boundary Traversal Output for Skewed Tree
What is the output of the boundary traversal for the following skewed binary tree?
DSA Typescript
// Tree structure:
// 1
//  \
//   2
//    \
//     3
//      \
//       4

class Node {
  constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {}
}

const root = new Node(1, null, new Node(2, null, new Node(3, null, new Node(4))));

console.log(boundaryTraversal(root));

function boundaryTraversal(root: Node | null): number[] {
  if (!root) return [];

  const result: number[] = [];

  function isLeaf(node: Node): boolean {
    return !node.left && !node.right;
  }

  function addLeftBoundary(node: Node | null) {
    let curr = node;
    while (curr) {
      if (!isLeaf(curr)) result.push(curr.data);
      if (curr.left) curr = curr.left;
      else curr = curr.right;
    }
  }

  function addLeaves(node: Node | null) {
    if (!node) return;
    if (isLeaf(node)) {
      result.push(node.data);
      return;
    }
    addLeaves(node.left);
    addLeaves(node.right);
  }

  function addRightBoundary(node: Node | null) {
    const temp: number[] = [];
    let curr = node;
    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.left);
  addLeaves(root);
  addRightBoundary(root.right);

  return result;
}
A[1, 2, 3, 4]
B[1, 4, 3, 2]
C[1, 4]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
In a skewed tree, left boundary might be empty; right boundary is reversed.
🚀 Application
expert
1:30remaining
Count Nodes in Boundary Traversal
Given a binary tree, how many nodes will be included in the boundary traversal if the tree is a perfect binary tree of height 3 (levels 0 to 3)?
A9
B12
C15
D10
Attempts:
2 left
💡 Hint
Count root, left boundary excluding leaves, leaves, and right boundary excluding leaves carefully.

Practice

(1/5)
1. What is the main purpose of Boundary Traversal in a binary tree?
easy
A. To print all leaf nodes only
B. To print all nodes on the boundary in anti-clockwise direction
C. To print nodes level by level
D. To print nodes in in-order traversal

Solution

  1. Step 1: Understand boundary traversal definition

    Boundary traversal prints nodes on the edges of the tree, including left boundary, leaves, and right boundary.
  2. Step 2: Identify traversal direction

    The traversal is done in anti-clockwise direction starting from root, left boundary, leaves, then right boundary.
  3. Final Answer:

    To print all nodes on the boundary in anti-clockwise direction -> Option B
  4. Quick Check:

    Boundary traversal = anti-clockwise boundary nodes [OK]
Hint: Boundary traversal prints edges anti-clockwise [OK]
Common Mistakes:
  • Confusing boundary traversal with leaf node traversal
  • Thinking it prints nodes level-wise
  • Assuming it prints in-order nodes
2. Which of the following TypeScript function signatures correctly represents a function to perform boundary traversal of a binary tree?
easy
A. function boundaryTraversal(): number[]
B. function boundaryTraversal(root: TreeNode): void
C. function boundaryTraversal(root: TreeNode, result: number[]): void
D. function boundaryTraversal(root: TreeNode): number[]

Solution

  1. Step 1: Analyze function purpose

    The function should take the root node and return an array of numbers representing boundary nodes.
  2. Step 2: Check signatures

    function boundaryTraversal(root: TreeNode): number[] takes root and returns number array, matching expected usage. function boundaryTraversal(root: TreeNode): void returns void, so no output. function boundaryTraversal(root: TreeNode, result: number[]): void requires external array input, less common. function boundaryTraversal(): number[] has no input, so invalid.
  3. Final Answer:

    function boundaryTraversal(root: TreeNode): number[] -> Option D
  4. Quick Check:

    Function returns boundary nodes array [OK]
Hint: Function should return array of boundary nodes [OK]
Common Mistakes:
  • Using void return type without output
  • Missing root parameter
  • Expecting external array input unnecessarily
3. Given the binary tree below, what is the boundary traversal output?
      1
     / \
    2   3
   / \   \
  4   5   6
     / \  /
    7  8 9
medium
A. [1, 2, 4, 7, 8, 9, 6]
B. [1, 2, 4, 5, 7, 8, 9, 6, 3]
C. [1, 2, 4, 7, 8, 9, 6, 3]
D. [1, 2, 4, 7, 8, 9, 3, 6]

Solution

  1. Step 1: Identify left boundary nodes

    Left boundary excluding leaves: 1, 2
  2. Step 2: Identify leaf nodes left to right

    Leaves: 4, 7, 8, 9, 6
  3. Step 3: Identify right boundary nodes bottom to top excluding leaves

    Right boundary: 3
  4. Step 4: Combine in anti-clockwise order

    Boundary = left boundary + leaves (excluding duplicates) + right boundary = [1, 2, 4, 7, 8, 9, 6, 3]
  5. Final Answer:

    [1, 2, 4, 7, 8, 9, 6, 3] -> Option C
  6. Quick Check:

    Boundary traversal = [1,2,4,7,8,9,6,3] [OK]
Hint: Combine left boundary, leaves, right boundary anti-clockwise [OK]
Common Mistakes:
  • Including leaf nodes twice
  • Wrong order of right boundary nodes
  • Missing root or boundary nodes
4. Consider the following TypeScript code snippet for boundary traversal. What is the bug?
function printBoundary(root: TreeNode | null) {
  if (!root) return;
  console.log(root.val);
  printLeftBoundary(root.left);
  printLeaves(root.left);
  printLeaves(root.right);
  printRightBoundary(root.right);
}

function printLeftBoundary(node: TreeNode | null) {
  if (!node || (!node.left && !node.right)) return;
  console.log(node.val);
  if (node.left) printLeftBoundary(node.left);
  else printLeftBoundary(node.right);
}

function printRightBoundary(node: TreeNode | null) {
  if (!node || (!node.left && !node.right)) return;
  if (node.right) printRightBoundary(node.right);
  else printRightBoundary(node.left);
  console.log(node.val);
}
medium
A. Left boundary includes leaf nodes which should be excluded
B. Root node is printed twice
C. Right boundary nodes are printed before recursive calls, causing wrong order
D. Leaves are printed twice because printLeaves is called on both left and right subtrees

Solution

  1. Step 1: Analyze left boundary function

    printLeftBoundary prints node.val even if node is leaf, but leaves should be excluded from left boundary.
  2. Step 2: Check leaf exclusion condition

    Condition (!node.left && !node.right) returns early, so leaves are excluded from printing in left boundary, so no bug here.
  3. Step 3: Re-examine leaf printing calls

    printLeaves is called separately on left and right subtrees, so leaves are printed once only.
  4. Step 4: Check right boundary printing order

    Right boundary prints after recursive calls, which is correct for bottom-up order.
  5. Step 5: Identify actual bug

    Left boundary function prints node.val before recursive call, so leaf nodes on left boundary get printed twice: once in left boundary and once in leaves.
  6. Final Answer:

    Left boundary includes leaf nodes which should be excluded -> Option A
  7. Quick Check:

    Leaf nodes must not print in left boundary [OK]
Hint: Exclude leaves from left/right boundary printing [OK]
Common Mistakes:
  • Printing leaves twice
  • Printing right boundary in wrong order
  • Printing root twice
5. You want to modify boundary traversal to print nodes in clockwise order starting from root. Which change is correct?
hard
A. Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up
B. Print root, then left boundary top-down, then leaves left to right, then right boundary bottom-up
C. Print root, then leaves left to right, then left boundary top-down, then right boundary bottom-up
D. Print root, then right boundary bottom-up, then leaves right to left, then left boundary top-down

Solution

  1. Step 1: Understand clockwise boundary traversal order

    Clockwise traversal starts at root, then right boundary top-down, leaves right to left, then left boundary bottom-up.
  2. Step 2: Match options with traversal order

    Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up matches this order exactly. Print root, then left boundary top-down, then leaves left to right, then right boundary bottom-up is anti-clockwise order. Options C and D mix orders incorrectly.
  3. Final Answer:

    Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up -> Option A
  4. Quick Check:

    Clockwise = root + right boundary + leaves + left boundary [OK]
Hint: Clockwise = root, right boundary, leaves, left boundary [OK]
Common Mistakes:
  • Confusing clockwise with anti-clockwise order
  • Printing boundaries in wrong direction
  • Mixing leaf traversal order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard