Print Tree Perimeter
Explore how to print the perimeter of a binary tree by following a specific order: left boundary, leaf nodes, and right boundary. Learn step-by-step methods using iterative and recursive techniques along with analyzing their time and space complexities. This lesson helps you implement perimeter traversal to handle tree boundary problems effectively.
Statement
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):
- Left boundary
- Leaf nodes
- Right boundary
Example
In the following tree, the nodes highlighted in green, form the perimeter.
The expected output for the tree above is: 100, 50, 25, 10, 70, 400, 350, 200.
Sample input
The input list below represents the level-order traversal of the binary tree:
[100, 50, 200, 25, 60, 350, 10, 70, 400]
Expected output
The sequence below represents the perimeter of the binary tree:
100, 50, 25, 10, 70, 400, 350, 200
Try it yourself
Note: The binary tree node’s class has members
leftand ...