Print Tree Perimeter
Given a binary tree, print its perimeter nodes.
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
left
andright
to store references to other nodes, along with the memberdata
to hold the node’s value.
main.cpp
BinaryTree.cpp
BinaryTreeNode.cpp
#include "BinaryTree.cpp"#include <stack>#include <string>using namespace std;void DisplayTreePerimeter(BinaryTreeNode* root) {// TODO: Write - Your - Codecout << "";}
...
Access this course and 1400+ top-rated courses and projects.