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.

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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single DFS traversal visits each node exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Only one recursion stack of height h, where h is the tree height

n
2n
โœ“ 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)
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