Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Boundary Traversal of Binary Tree in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Boundary Traversal of Binary Tree
Start at root
↓
Traverse left boundary
↓
Traverse leaf nodes
↓
Traverse right boundary bottom-up
↓
Combine all nodes for boundary traversal
The traversal collects nodes from the left edge top-down, then all leaves left to right, then the right edge bottom-up, combining them for the boundary.
Execution Sample
DSA Typescript
function boundaryTraversal(root: TreeNode | null): number[] {
  if (!root) return [];
  const result: number[] = [root.val];

  const isLeaf = (node: TreeNode | null): boolean => node !== null && node.left === null && node.right === null;

  // Traverse left boundary
  let curr: TreeNode | null = root.left;
  while (curr && (curr.left || curr.right)) {
    result.push(curr.val);
    curr = curr.left ? curr.left : curr.right;
  }

  // Traverse leaves
  const addLeaves = (node: TreeNode | null): void => {
    if (!node) return;
    if (isLeaf(node)) {
      result.push(node.val);
      return;
    }
    addLeaves(node.left);
    addLeaves(node.right);
  };
  addLeaves(root.left);
  addLeaves(root.right);

  // Traverse right boundary
  const rightStack: number[] = [];
  curr = root.right;
  while (curr && (curr.left || curr.right)) {
    rightStack.push(curr.val);
    curr = curr.right ? curr.right : curr.left;
  }
  while (rightStack.length) {
    result.push(rightStack.pop()!);
  }

  return result;
}
This code outlines the steps to collect boundary nodes of a binary tree in order.
Execution Table
StepOperationNode VisitedPointer ChangesTree State (Boundary Nodes Collected)
1Start at root1root -> 1[1]
2Traverse left boundary2current -> 2[1, 2]
3Traverse left boundary4current -> 4[1, 2, 4]
4Traverse left boundarynull (leaf or no left/right child)stop[1, 2, 4]
5Traverse leaf nodes8visit leaf[1, 2, 4, 8]
6Traverse leaf nodes9visit leaf[1, 2, 4, 8, 9]
7Traverse leaf nodes5visit leaf[1, 2, 4, 8, 9, 5]
8Traverse leaf nodes6visit leaf[1, 2, 4, 8, 9, 5, 6]
9Traverse right boundary bottom-up3stack push 3[1, 2, 4, 8, 9, 5, 6]
10Traverse right boundary bottom-up7stack push 7[1, 2, 4, 8, 9, 5, 6]
11Traverse right boundary bottom-upnull (no right/left child)stop[1, 2, 4, 8, 9, 5, 6]
12Add right boundary nodes from stack7pop 7[1, 2, 4, 8, 9, 5, 6, 7]
13Add right boundary nodes from stack3pop 3[1, 2, 4, 8, 9, 5, 6, 7, 3]
14Traversal completenullend[1, 2, 4, 8, 9, 5, 6, 7, 3]
💡 All boundary nodes collected: left boundary, leaves, right boundary bottom-up
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 11After Step 14
result[][1, 2][1, 2, 4][1, 2, 4, 8, 9, 5, 6][1, 2, 4, 8, 9, 5, 6][1, 2, 4, 8, 9, 5, 6, 7, 3]
current (left boundary)124nullnullnull
stack (right boundary)[][][][][3,7][]
Key Moments - 3 Insights
Why do we not include leaf nodes when traversing the left and right boundaries?
Leaf nodes are collected separately in the leaf traversal step to avoid duplicates, as they can appear in both boundary and leaf traversals. See execution_table rows 5-8 for leaf collection.
Why is the right boundary collected bottom-up using a stack?
Right boundary nodes are added in reverse order to maintain correct traversal order. We push them while traversing down, then pop to add them bottom-up. See execution_table rows 9-13.
What happens if the tree has only one node?
The root is the only boundary node, so traversal ends after adding root. This is shown in execution_table row 1 and exit note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what nodes are collected after step 7?
A[1, 2, 4, 8, 9, 5, 6]
B[1, 2, 4]
C[1, 2, 4, 8, 9]
D[1, 2, 4, 8, 9, 5]
💡 Hint
Check the 'Tree State' column at step 7 in execution_table.
At which step does the traversal start adding right boundary nodes to the result?
AStep 9
BStep 10
CStep 12
DStep 14
💡 Hint
Look for when nodes are popped from the stack in execution_table.
If the tree had no left child, how would the left boundary traversal steps change?
ALeft boundary traversal would continue to leaves.
BLeft boundary traversal would stop immediately after root.
CLeft boundary traversal would add all nodes.
DLeft boundary traversal would add right boundary nodes.
💡 Hint
Refer to variable_tracker for 'current (left boundary)' changes and execution_table steps 2-4.
Concept Snapshot
Boundary Traversal of Binary Tree:
- Start at root
- Traverse left boundary (excluding leaves) top-down
- Traverse all leaf nodes left to right
- Traverse right boundary (excluding leaves) bottom-up
- Combine all nodes in order
- Use stack for right boundary to reverse order
Full Transcript
Boundary traversal collects nodes around the edges of a binary tree. It starts at the root, then collects the left boundary nodes top-down excluding leaves, then all leaf nodes from left to right, and finally the right boundary nodes bottom-up excluding leaves. The right boundary is collected using a stack to reverse the order. This ensures no duplicates and correct traversal order. The final list combines these nodes to represent the boundary of the tree.

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