Boundary traversal of binary tree in C++
The process of visiting the boundary nodes of a binary tree in a particular order is referred to as boundary traversal. The left boundary, which does not include the left leaf nodes, leaf nodes, and the right boundary, which does not include the right leaf nodes, make up the border nodes. You may access and process the tree's external nodes with the aid of this traversal, which is helpful for a variety of applications.
 Left Boundary: The group of nodes from the tree's root to its leftmost nodeaside from the left leaf nodesforms the left boundary. It is possible to implement the left boundary traversal from the top down. Anytime there is a left child, you start at the root and go there. If there isn't a left child, visit the right child and keep going until you find the leftmost node.
 Leaf Nodes: Nodes that have no children on either the left or the right are known as leaf nodes. Utilizing an inorder traversal, you can move through these nodes. Each leaf node will be visited during an inorder traverse, which travels clockwise.
 Right Boundary: The set of nodes that make up the right boundary of the tree, omitting the right leaf nodes, extends from the rightmost node to the root. You can construct the traversal of the right boundary using a bottomup method, just like you did for the left boundary. Every time a suitable youngster is present, you start at the source and go there. You visit the left child if there isn't a right child, then carry on this manner until you reach the rightmost node.
Boundary traversal can be implemented in C++ by creating distinct functions for left boundary traversal, right boundary traversal, and leaf node traversal. These three elements will be included in the final boundary traversal in the stated order.
The steps for boundary traversal are listed below in brief:
 The root node should be printed.
 Use a topdown strategy to print the left boundary (excluding leftleaf nodes).
 Using an inorder traverse, print the leaf nodes.
 Use a bottomup strategy to print the right boundary (excluding the right leaf nodes).
Code:
Let's take an example to illustrate the boundary traversal of binary tree in C++:
Output:
Explanation:
1. In this example, the code starts by declaring the TreeNode structure and includes the required header file. The three properties of this structure, which stand in for a node in a binary tree, are left, which points to the node's left child, right, which points to the node's right child, and val, which stores the node's value.
Three recursive functions are defined in the code for various aspects of boundary traversal.
 printLeaves(TreeNode* root): The function printLeaves(TreeNode* root) is responsible for publishing the tree's leaf nodes. It moves through the tree from left to right, starting at the root, and prints a node's value if it is a leaf node (that is if it lacks both left and right children).
 printLeftBoundary(TreeNode* root): Except for leftleaf nodes, the left boundary of the tree can be printed using the printLeftBoundary(TreeNode* root) Starting at the root and moving to the leftmost node, it employs a topdown strategy. A node prints its left child if it has one; otherwise, the right child is taken into account. The process keeps on until it reaches the leftmost nonleaf node.
 printRightBoundary(TreeNode* root): Similar to printLeftBoundary, printRightBoundary prints the right boundary of the tree (excluding right leaf nodes) from the bottom up. The function name is printRightBoundary(TreeNode* root). Printing nodes along the way, start from the root and move to the rightmost nonleaf node.
2. The primary engine behind boundary traversal is the function boundaryTraversal(TreeNode* root). The root node is displayed first. After that, it calls printLeftBoundary to print the left boundary (excluding the leaf nodes on the left), printLeaves to print the leaf nodes, and printRightBoundary to print the right boundary (excluding the leaf nodes on the right).
For purposes of demonstration, a sample binary tree is built in the main function. The structure of the tree is as follows:
3. The boundary traversal is carried out by calling the boundaryTraversal function with the tree root.
The binary tree's nodes are printed in the following order by the code: root, left boundary (apart from left leaf nodes), leaf nodes, and right boundary (aside from right leaf nodes).
