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.

Input: Root of a binary tree
Output: 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
Binary Tree Boundary Traversal123456789ROOTLEFT BOUNDARYLEAVES (L→R)RIGHT BOUNDARYTraversal Steps1Root: Add root node (1)2Left: Add left boundary (2, 4)3Leaves: Add leaves L→R (8, 9, 5, 6)4Right: Add right boundary reversed (7, 3)Final Boundary: [1, 2, 4, 8, 9, 5, 6, 7, 3]
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.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 23 Apple 15
73.8K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen