Binary Tree · DFS · BFS
Boundary of Binary Tree
Return the boundary nodes of a binary tree in anti-clockwise order: left boundary → leaves → right boundary (reversed).
Key concepts at a glance — for those who already know the basics.
Problem Statement
Return the values of the boundary nodes of a binary tree in anti-clockwise order starting from the root.
Boundary includes the left boundary (excluding leaves), all leaves from left to right, and the right boundary in reverse (excluding leaves).
Example output: [1, 2, 4, 8, 9, 10, 6, 7, 3]
01 · The definition
Boundary = Left Boundary (top→down, no leaves) + All Leaves (left→right) + Right Boundary (bottom→up, no leaves)
The root is always included. Nodes are collected anti-clockwise so the output "traces the perimeter" of the tree.
02 · The key insight
03 · Visual — how the boundary is traced
Output for this tree: [1, 2, 4, 8, 9, 10, 6, 7, 3] — root, left boundary (2,4), all leaves left-to-right (8,9,10,6), right boundary reversed (7,3).
04 · Code (Python)
05 · Dry run — tree [1,2,3,4,5,6,7,8,9,10]
06 · Complexity
Practice on LeetCode
Try #199 (Right Side View) first — simpler boundary concept to build intuition.