Boundary Traversal of Binary Tree in DSA Javascript - 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.
Specifically, how does the number of nodes affect the work done?
Analyze the time complexity of the following code snippet.
function boundaryTraversal(root) {
if (!root) return [];
const result = [];
function leftBoundary(node) {
if (!node || (!node.left && !node.right)) return;
result.push(node.val);
if (node.left) leftBoundary(node.left);
else leftBoundary(node.right);
}
function leaves(node) {
if (!node) return;
leaves(node.left);
if (!node.left && !node.right) result.push(node.val);
leaves(node.right);
}
function rightBoundary(node) {
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 in the tree.
- How many times: The tree nodes are visited a constant number of times each (at most twice).
As the number of nodes (n) increases, the total number of node visits is proportional to n, so the work grows linearly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The number of operations grows linearly with the number of nodes.
Time Complexity: O(n)
This means the time to do the boundary traversal grows in direct proportion to the number of nodes in the tree.
[X] Wrong: "The boundary traversal only visits boundary nodes, so it must be faster than visiting all nodes."
[OK] Correct: The code still visits many nodes to find leaves and edges, so it ends up visiting every node in the tree.
Understanding how tree traversals scale helps you explain your approach clearly and shows you know how to handle data structures efficiently.
"What if we changed the code to use iterative traversal with stacks 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: Compare options with definition
Only To print all nodes on the boundary in an anti-clockwise direction describes printing all boundary nodes in anti-clockwise order.Final Answer:
To print all nodes on the boundary in an anti-clockwise direction -> Option BQuick Check:
Boundary traversal = print boundary nodes anti-clockwise [OK]
- Confusing boundary traversal with leaf node traversal
- Thinking it prints nodes level-wise
- Mixing it with in-order traversal
Solution
Step 1: Check function signature and base case
The function should accept root and handle empty tree by returning empty array.Step 2: Validate options
function boundaryTraversal(root) { if (!root) return []; } correctly defines function with root parameter and returns empty array if root is null.Final Answer:
function boundaryTraversal(root) { if (!root) return []; } -> Option CQuick Check:
Function starts with root check and empty return [OK]
- Omitting root parameter
- Not handling empty tree
- Returning wrong values at start
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9What is the output of boundary traversal (anti-clockwise)?
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-up excluding leaves
Right boundary: 3Step 4: Combine in anti-clockwise order
Boundary traversal: 1 (root), 2 (left boundary), 4 (leaf), 7, 8, 9, 6 (leaf), 3 (right boundary)Final Answer:
[1, 2, 4, 7, 8, 9, 6, 3] -> Option DQuick Check:
Boundary nodes combined anti-clockwise = [1, 2, 4, 7, 8, 9, 6, 3] [OK]
- Including internal nodes as leaves
- Adding right boundary leaves twice
- Wrong order of nodes
function printLeaves(node, result) {
if (!node) return;
if (!node.left && !node.right) {
result.push(node.data);
}
printLeaves(node.left, result);
printLeaves(node.right, result);
}What is the bug in this code?
Solution
Step 1: Analyze leaf detection and recursion order
Code checks if node is leaf and pushes data, then recursively visits left then right child.Step 2: Verify leaf order correctness
Visiting left subtree before right ensures leaves are collected left to right.Step 3: Check for bugs
No bug found; code correctly collects leaves in left to right order.Final Answer:
It visits leaves in left to right order correctly -> Option AQuick Check:
Leaf nodes collected left to right = It visits leaves in left to right order correctly [OK]
- Thinking recursion order is reversed
- Assuming non-leaf nodes are pushed
- Missing base case check
Solution
Step 1: Understand boundary traversal parts
Boundary traversal includes left boundary, leaves, and right boundary.Step 2: Identify where to exclude even leaves
Only leaf nodes need filtering; boundaries remain unchanged.Step 3: Adapt leaf collection function
Modify leaf collection to check leaf value and skip if even.Final Answer:
Modify leaf collection to skip leaves with even values during traversal -> Option AQuick Check:
Filter leaves during traversal, not boundaries [OK]
- Filtering boundary nodes incorrectly
- Filtering after traversal causing extra work
- Ignoring leaf filtering altogether
Ready to prove it?
Pass the test and this pattern moves unknown
