Boundary Traversal of Binary Tree in DSA C# - 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 the edges of a binary tree?
Analyze the time complexity of the following code snippet.
// Boundary traversal of a binary tree
void BoundaryTraversal(Node root) {
if (root == null) return;
Console.Write(root.data + " ");
PrintLeftBoundary(root.left);
PrintLeaves(root.left);
PrintLeaves(root.right);
PrintRightBoundary(root.right);
}
This code prints the boundary nodes of a binary tree in anti-clockwise order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive traversal of nodes to print left boundary, leaves, and right boundary.
- How many times: Each node is visited at most once during these traversals.
As the number of nodes (n) in the tree grows, the traversal visits each node once or skips it.
| 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 only visits a few nodes, so it runs in constant time."
[OK] Correct: Even though only boundary nodes are printed, the algorithm must check many nodes to find leaves and boundaries, so it still visits most nodes.
Understanding this traversal's time complexity helps you explain how tree traversals work and why visiting nodes carefully matters in coding interviews.
"What if we changed the traversal to only print leaves without boundaries? How would the time complexity change?"
Practice
Boundary Traversal of a binary tree include?Solution
Step 1: Understand boundary traversal components
Boundary traversal includes nodes on the left edge, all leaf nodes, and nodes on the right edge.Step 2: Combine these parts in order
Traverse left boundary top-down, then leaves left to right, then right boundary bottom-up.Final Answer:
Left boundary, leaves, and right boundary nodes in order -> Option BQuick Check:
Boundary traversal = left + leaves + right [OK]
- Including root multiple times
- Missing leaf nodes
- Reversing right boundary order
root and returns a list of integers?Solution
Step 1: Check return type and parameter
Boundary traversal returns a list of node values, so return type should be List<int> and parameter should be Node.Step 2: Validate method signature syntax
public List<int> BoundaryTraversal(Node root) matches correct syntax: public List<int> BoundaryTraversal(Node root)Final Answer:
public List<int> BoundaryTraversal(Node root) -> Option CQuick Check:
Return list<int> and input Node root [OK]
- Using void return type
- Wrong parameter type (int instead of Node)
- Missing generic type in List
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9What is the boundary traversal output?
Solution
Step 1: Identify left boundary nodes
Left boundary (excluding leaves): 1, 2Step 2: Identify leaf nodes left to right
Leaves: 4, 7, 8, 9Step 3: Identify right boundary nodes bottom-up
Right boundary (excluding leaves): 3, 6Step 4: Combine nodes without duplicates
Boundary traversal: 1, 2, 4, 7, 8, 9, 3, 6Final Answer:
[1, 2, 4, 7, 8, 9, 3, 6] -> Option AQuick Check:
Left + leaves + right = [1,2,4,7,8,9,3,6] [OK]
- Including leaf nodes twice
- Missing right boundary node 3
- Wrong order of leaves
void PrintLeftBoundary(Node node) {
if (node == null) return;
Console.Write(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 recursive call even if node is leaf, so leaf nodes get printed here.Step 2: Understand boundary traversal rules
Left boundary function should exclude leaf nodes to avoid duplicates in leaf printing step.Final Answer:
It prints leaf nodes which should be excluded -> Option DQuick Check:
Left boundary excludes leaves [OK]
- Printing leaves in left boundary
- Not checking for null nodes
- Confusing left and right child checks
Solution
Step 1: Avoid duplicates by excluding leaves in boundaries
Exclude leaves from left and right boundary to prevent duplicates with leaf traversal.Step 2: Handle root separately
Print root once at start, exclude it from right boundary to avoid repetition.Step 3: Combine all parts carefully
Print root, left boundary (no leaves), leaves (excluding root), right boundary (no leaves or root) for correct order and no duplicates.Final Answer:
Print root, then left boundary excluding leaves, then leaves excluding root, then right boundary excluding leaves and root -> Option AQuick Check:
Exclude duplicates by careful boundary and leaf separation [OK]
- Printing root multiple times
- Including leaves in boundaries causing duplicates
- Ignoring skewed tree edge cases
Ready to prove it?
Pass the test and this pattern moves unknown
