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.
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)
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code