Given the root node of a binary tree, print the nodes that form its perimeter (boundary). We must print the perimeter of the binary tree in three phases (order is important):

  1. Left boundary
  2. Leaf nodes
  3. Right boundary


In the following tree, the nodes highlighted in green, form the perimeter.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.