Boundary of Binary Tree - Problem
Boundary of Binary Tree is a fascinating tree traversal problem that challenges you to trace the perimeter of a binary tree like drawing its outline on paper.
Imagine you're looking at a binary tree from above and want to trace its boundary clockwise starting from the root. The boundary consists of four distinct parts:
๐ณ Root node (starting point)
๐ฝ Left boundary (leftmost path, excluding leaves)
๐ All leaf nodes (from left to right)
๐ผ Right boundary (rightmost path in reverse, excluding leaves)
The left boundary follows these rules: start with root's left child, then keep going left if possible, otherwise go right. Stop before reaching a leaf. The right boundary follows similar rules but on the right side, and we traverse it in reverse order.
Goal: Given a binary tree root, return an array containing the boundary node values in clockwise order.
Imagine you're looking at a binary tree from above and want to trace its boundary clockwise starting from the root. The boundary consists of four distinct parts:
๐ณ Root node (starting point)
๐ฝ Left boundary (leftmost path, excluding leaves)
๐ All leaf nodes (from left to right)
๐ผ Right boundary (rightmost path in reverse, excluding leaves)
The left boundary follows these rules: start with root's left child, then keep going left if possible, otherwise go right. Stop before reaching a leaf. The right boundary follows similar rules but on the right side, and we traverse it in reverse order.
Goal: Given a binary tree root, return an array containing the boundary node values in clockwise order.
Input: Root of a binary treeOutput: Array of integers representing boundary values in clockwise order Input & Output
example_1.py โ Standard Binary Tree
$
Input:
root = [1,2,3,4,5,6,null,null,null,7,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
โบ
Output:
[1,2,4,7,11,12,13,10,9,3]
๐ก Note:
The boundary traces clockwise: start with root(1), go down left boundary(2,4,7,11), collect leaves(12,13,10,9), then up right boundary(3). Note that leaves are collected left-to-right, and right boundary is in reverse order.
example_2.py โ Simple Tree
$
Input:
root = [1,2,3,4,5,null,6,7,8,null,9,null,10,null,11]
โบ
Output:
[1,2,4,7,8,9,11,10,6,3]
๐ก Note:
Root(1) โ Left boundary(2,4,7) โ Leaves left-to-right(8,9,11,10,6) โ Right boundary reversed(3). The left boundary stops at 7 since it's not a leaf, and leaves include all nodes without children.
example_3.py โ Edge Case: Only Root
$
Input:
root = [1]
โบ
Output:
[1]
๐ก Note:
When tree has only root node, the boundary is just the root itself. Root is considered the entire boundary, not a leaf in this context.
Visualization
Tap to expand
Understanding the Visualization
1
Start at Root
Begin at the root node - this is always part of the boundary
2
Left Boundary
Go down the leftmost path, adding nodes as we descend (pre-order)
3
Collect Leaves
Gather all leaf nodes from left to right using in-order traversal
4
Right Boundary
Go up the rightmost path, adding nodes as we backtrack (post-order)
Key Takeaway
๐ฏ Key Insight: Use a single DFS with boundary flags - left boundary in pre-order, leaves in in-order, right boundary in post-order for optimal O(n) time complexity.
Time & Space Complexity
Time Complexity
O(n)
Single DFS traversal visits each node exactly once
โ Linear Growth
Space Complexity
O(h)
Only one recursion stack of height h, where h is the tree height
โ Linear Space
Constraints
- The number of nodes in the tree is in the range [1, 104]
- -1000 โค Node.val โค 1000
- The root is guaranteed to not be a leaf (unless it's the only node)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code