Boundary Traversal of Binary Tree in DSA Java - Time & Space Complexity
We want to understand how the time needed to do a boundary traversal grows as the tree gets bigger.
Specifically, how many steps does it take to visit all boundary nodes in a binary tree?
Analyze the time complexity of the following code snippet.
public void boundaryTraversal(Node root) {
if (root == null) return;
System.out.print(root.data + " ");
printLeftBoundary(root.left);
printLeaves(root.left);
printLeaves(root.right);
printRightBoundary(root.right);
}
private void printLeftBoundary(Node node) {
while (node != null) {
if (!isLeaf(node)) System.out.print(node.data + " ");
node = (node.left != null) ? node.left : node.right;
}
}
private boolean isLeaf(Node node) {
return (node.left == null && node.right == null);
}
This code prints the boundary nodes of a binary tree: left edge, leaves, and right edge.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing nodes in left boundary, leaves, and right boundary.
- How many times: Each node is visited at most twice during these traversals.
As the number of nodes (n) grows, the code visits each node once or twice but never more.
| 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: "Boundary traversal takes more than linear time because it does multiple traversals."
[OK] Correct: Even though it looks like multiple steps, each node is visited only a few times, so total work is still proportional to n.
Understanding this helps you explain how tree traversals work efficiently and shows you can analyze combined traversals clearly.
"What if we used recursion instead of loops for left and right boundary? How would the time complexity change?"
Practice
Solution
Step 1: Understand boundary traversal parts
Boundary traversal visits root, then left boundary top-down, then leaves left to right, then right boundary bottom-up.Step 2: Arrange traversal order
Root is first, left boundary next, leaves after that, and right boundary last in reverse order.Final Answer:
Root, left boundary (top-down), leaves (left to right), right boundary (bottom-up) -> Option CQuick Check:
Boundary traversal order = Root + Left + Leaves + Right [OK]
- Mixing order of leaves and boundaries
- Visiting right boundary top-down instead of bottom-up
- Skipping root node
Solution
Step 1: Recall Java method syntax
Java methods start with access modifier, then return type, then method name and parameters.Step 2: Match correct syntax
public void boundaryTraversal(Node root) matches: public void boundaryTraversal(Node root). Others have wrong order or missing parts.Final Answer:
public void boundaryTraversal(Node root) -> Option DQuick Check:
Access modifier + return type + method name + parameters [OK]
- Placing return type after method name
- Missing access modifier
- Incorrect parameter placement
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
Solution
Step 1: Identify boundary nodes
Root: 1; Left boundary top-down: 2 (excluding leaf 4); Leaves left to right: 4,7,8,9; Right boundary bottom-up: 6,3 (excluding leaves).Step 2: Combine nodes without duplicates
Boundary traversal: 1 (root), 2 (left boundary), 4,7,8,9 (leaves), 6,3 (right boundary).Final Answer:
[1, 2, 4, 7, 8, 9, 6, 3] -> Option BQuick Check:
Boundary traversal output = [1,2,4,7,8,9,6,3] [OK]
- Including leaf nodes twice
- Wrong order of right boundary nodes
- Missing leaf nodes
void printLeftBoundary(Node node) {
if (node == null) return;
System.out.print(node.data + " ");
if (node.left != null) {
printLeftBoundary(node.left);
} else if (node.right != null) {
printLeftBoundary(node.right);
}
}
What is the bug in this code?Solution
Step 1: Analyze leaf node printing
The code prints node data before checking for children, thus printing leaf nodes as well.Step 2: Identify leaf node exclusion missing
Leaf nodes should not be printed in left boundary function, but this code prints them unconditionally after null check.Final Answer:
It prints leaf nodes which should be excluded in left boundary -> Option AQuick Check:
Left boundary excludes leaves, code prints them [OK]
- Not checking if node is leaf before printing
- Confusing left and right boundary logic
- Missing base case for null nodes
Solution
Step 1: Understand tree structure
Since leaves are only on rightmost path and some nodes have only right children, left boundary may be missing or partial.Step 2: Modify traversal rules
Skip left boundary if no left child; print leaves once; print right boundary bottom-up excluding leaves to avoid duplicates.Step 3: Combine for correct boundary
This ensures no duplicates and correct order for such skewed trees.Final Answer:
Skip printing left boundary if no left child; print leaves only once; print right boundary bottom-up excluding leaves -> Option AQuick Check:
Handle missing left boundary and leaf duplicates carefully [OK]
- Printing leaves multiple times
- Ignoring missing left boundary
- Printing right boundary top-down causing wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
