Boundary Traversal of Binary Tree in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to do a boundary traversal of a binary tree changes as the tree grows.
The question is: how does the number of steps grow when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function boundaryTraversal(root: TreeNode | null): number[] {
if (!root) return [];
const result: number[] = [];
function leftBoundary(node: TreeNode | null) {
if (!node || (!node.left && !node.right)) return;
result.push(node.val);
if (node.left) leftBoundary(node.left);
else leftBoundary(node.right);
}
function leaves(node: TreeNode | null) {
if (!node) return;
leaves(node.left);
if (!node.left && !node.right) result.push(node.val);
leaves(node.right);
}
function rightBoundary(node: TreeNode | null) {
if (!node || (!node.left && !node.right)) return;
if (node.right) rightBoundary(node.right);
else rightBoundary(node.left);
result.push(node.val);
}
result.push(root.val);
leftBoundary(root.left);
leaves(root.left);
leaves(root.right);
rightBoundary(root.right);
return result;
}
This code collects the boundary nodes of a binary tree in order: left edge, leaves, then right edge.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive traversal of nodes to collect left boundary, leaves, and right boundary.
- How many times: Each node is visited at most once during these traversals.
As the number of nodes in the tree increases, the code visits each node at most once to check if it belongs to the boundary.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of steps grows roughly in direct proportion to the number of nodes.
Time Complexity: O(n)
This means the time to do the boundary traversal grows linearly with the number of nodes in the tree.
[X] Wrong: "The boundary traversal only visits a few nodes, so it runs in constant time."
[OK] Correct: Even though only boundary nodes are collected, the code must check many nodes to find leaves and boundaries, so it still visits all nodes.
Understanding this traversal helps you reason about tree algorithms and recursion, skills that are useful in many coding challenges and real projects.
"What if we changed the code to use an iterative approach with a stack instead of recursion? How would the time complexity change?"
Practice
Boundary Traversal in a binary tree?Solution
Step 1: Understand boundary traversal definition
Boundary traversal prints nodes on the edges of the tree, including left boundary, leaves, and right boundary.Step 2: Identify traversal direction
The traversal is done in anti-clockwise direction starting from root, left boundary, leaves, then right boundary.Final Answer:
To print all nodes on the boundary in anti-clockwise direction -> Option BQuick Check:
Boundary traversal = anti-clockwise boundary nodes [OK]
- Confusing boundary traversal with leaf node traversal
- Thinking it prints nodes level-wise
- Assuming it prints in-order nodes
Solution
Step 1: Analyze function purpose
The function should take the root node and return an array of numbers representing boundary nodes.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.Final Answer:
function boundaryTraversal(root: TreeNode): number[] -> Option DQuick Check:
Function returns boundary nodes array [OK]
- Using void return type without output
- Missing root parameter
- Expecting external array input unnecessarily
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
Solution
Step 1: Identify left boundary nodes
Left boundary excluding leaves: 1, 2Step 2: Identify leaf nodes left to right
Leaves: 4, 7, 8, 9, 6Step 3: Identify right boundary nodes bottom to top excluding leaves
Right boundary: 3Step 4: Combine in anti-clockwise order
Boundary = left boundary + leaves (excluding duplicates) + right boundary = [1, 2, 4, 7, 8, 9, 6, 3]Final Answer:
[1, 2, 4, 7, 8, 9, 6, 3] -> Option CQuick Check:
Boundary traversal = [1,2,4,7,8,9,6,3] [OK]
- Including leaf nodes twice
- Wrong order of right boundary nodes
- Missing root or boundary nodes
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);
}Solution
Step 1: Analyze left boundary function
printLeftBoundary prints node.val even if node is leaf, but leaves should be excluded from left boundary.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.Step 3: Re-examine leaf printing calls
printLeaves is called separately on left and right subtrees, so leaves are printed once only.Step 4: Check right boundary printing order
Right boundary prints after recursive calls, which is correct for bottom-up order.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.Final Answer:
Left boundary includes leaf nodes which should be excluded -> Option AQuick Check:
Leaf nodes must not print in left boundary [OK]
- Printing leaves twice
- Printing right boundary in wrong order
- Printing root twice
Solution
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.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.Final Answer:
Print root, then right boundary top-down, then leaves right to left, then left boundary bottom-up -> Option AQuick Check:
Clockwise = root + right boundary + leaves + left boundary [OK]
- Confusing clockwise with anti-clockwise order
- Printing boundaries in wrong direction
- Mixing leaf traversal order
Ready to prove it?
Pass the test and this pattern moves unknown
