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
Boundary of Binary Tree INPUT 1 2 3 4 5 6 7 9 10 Root Left Bnd Right Bnd Leaves ALGORITHM (DFS) 1 Add Root Start with node 1 2 Left Boundary DFS left: 2, 4 (no leaves) 3 Collect Leaves L-to-R: 7,11,12,13,10,9 4 Right Boundary Reverse: 3 (no leaves) Boundary Path Order Root: [1] Left: [2, 4] Leaves: [7,11,12,13,10,9] Right: [3] Combine all in order FINAL RESULT 1 2 3 4 7 9 10 [1,2,4,7,11,12,13,10,9,3] OK - Boundary Complete! 10 nodes in clockwise order Key Insight: The boundary traversal requires careful handling of three separate DFS traversals: 1) Left boundary goes down-left preferring left children (excluding leaves) 2) Leaves are collected left-to-right using standard DFS traversal 3) Right boundary is collected in reverse order (bottom-up) to maintain clockwise direction TutorialsPoint - Boundary of Binary Tree | DFS Approach
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