Boundary of Binary Tree
Return the anticlockwise boundary: root, left edge (top-down), all leaves (left-to-right), right edge (bottom-up) — no duplicates.
Open on LeetCode ↗Intuition
The boundary is three separate walks stitched together: slide down the left spine, collect leaves with a normal DFS, slide down the right spine but emit it reversed. The finicky part is exclusion — spine nodes that are leaves belong to the leaf pass only, and the root is emitted once up front.
Approach
Three collectors
leftBoundary: follow left (else right) from root.left, skipping leaves. leaves: DFS all. rightBoundary: follow right (else left) from root.right, skipping leaves, then reverse.
Dedup by role
Root printed first (unless it's a leaf, then the leaf pass owns it); spine passes skip leaves; leaf pass skips nothing — every boundary node has exactly one owner.
Stitch
[root] + left + leaves + reversed(right).
Solution & live demo
Edge cases
Missing spine contributes nothing; leaves and the existing spine still cover the outline.
Root is a leaf → emit once, not twice.