Boundary of Binary Tree

Medium · Trees · about 30 minutes

Asked at Meta, Microsoft, Amazon, Snowflake, Google, CrowdStrike.

Return the values on the boundary of a binary tree, anti-clockwise from the root: the root, then the left boundary, then the leaves from left to right, then the right boundary in reverse.

  • The left boundary is the path from the root's left child that always goes left if possible, otherwise

right, excluding the leaf it ends at. If the root has no left child, it's empty.

  • The right boundary is defined the same way on the right side (preferring right children).
  • Leaves are nodes with no children; the root is never counted as a leaf.

Related guides