# Boundary Root to Leaf Path traversal of a Binary Tree

Given a Binary Tree, the task is to print all Root to Leaf path of this tree in Boundary Root to Leaf path traversal.

Boundary Root to Leaf Path Traversal: In this traversal, the first Root to Leaf path(Left boundary) is printed first, followed by the last Root to Leaf path (Right boundary) in Reverse order. Then the process is repeated for the second and second-last Root to Leaf path, till the all Root to Leaf path has been printed.

Examples:

```
Input:
1
/  \
15    13
/     /   \
11    7     29
\    /
2   3
Output:  1 15 11
3 29 13 1
1 13 7 2

Explanation:
First of all print first path from Root to the Leaf
which is 1 15 11
Then, print the last path from the Leaf to Root
which is 3 29 13 1
Then, print the second path from Root to Leaf
which is 1 13 7 2

Input:
7
/  \
23     41
/  \      \
31   16     3
/ \     \    /
2   5    17  11
/
23

Output:  7 23 31 2
11 3 41 7
7 23 31 5
23 17 16 23 7

```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: In order to print paths in Boundary Root to Leaf Path Traversal,

• We first need to make Preorder Traversal of the binary tree to get the all node values of particular path.
• Here an array is used to store the path of the tree while doing the Preorder traversal then store this path into matrix.
• Then for each path, print the matrix in first row (Left to Right) then last row (Right to Left) and so on.

Below is the implementation of the above approach:

 `// C++ implementation to print the  ` `// path in Boundary Root to Leaf  ` `// Path Traversal. ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Structure of tree node ` `struct` `Node { ` `    ``int` `key; ` `    ``struct` `Node *left, *right; ` `}; ` ` `  `// Utility function to ` `// create a new node ` `Node* newNode(``int` `key) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->key = key; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `(temp); ` `} ` `// Function to calculate the length ` `// of longest path of the tree ` `int` `lengthOfLongestPath(``struct` `Node* node) ` `{ ` `    ``// Base Case ` `    ``if` `(node == NULL) ` `        ``return` `0; ` ` `  `    ``// Recursive call to calculate the length ` `    ``// of longest path ` `    ``int` `left = lengthOfLongestPath(node->left); ` `    ``int` `right = lengthOfLongestPath(node->right); ` ` `  `    ``return` `1 + (left > right ? left : right); ` `} ` ` `  `// Function to copy the complete ` `// path in a matrix ` `void` `copyPath(``int``* path, ``int` `index, ` `              ``int``** mtrx, ``int` `row) ` `{ ` `    ``// Loop to copy the path ` `    ``for` `(``int` `i = 0; i < index; i++) { ` `        ``mtrx[row][i] = path[i]; ` `    ``} ` `} ` ` `  `// Function to store all path ` `// one by one in matrix ` `void` `storePath(``struct` `Node* node, ` `               ``int``* path, ``int` `index, ` `               ``int``** mtrx, ``int``& row) ` `{ ` `    ``// Base condition ` `    ``if` `(node == NULL) { ` `        ``return``; ` `    ``} ` ` `  `    ``// Inserting the current node ` `    ``// into the current path ` `    ``path[index] = node->key; ` ` `  `    ``// Recursive call for ` `    ``// the left sub tree ` `    ``storePath(node->left, path, ` `              ``index + 1, mtrx, row); ` ` `  `    ``// Recursive call for ` `    ``// the right sub tree ` `    ``storePath(node->right, path, ` `              ``index + 1, mtrx, row); ` ` `  `    ``// Condition to check that current ` `    ``// node is a leaf node or not ` `    ``if` `(node->left == NULL ` `        ``&& node->right == NULL) { ` ` `  `        ``// Increamentation for changing ` `        ``// row ` `        ``row = row + 1; ` `        ``// Function call for copying  ` `        ``// the path ` `        ``copyPath(path, index + 1,  ` `                 ``mtrx, row); ` `    ``} ` `} ` ` `  `// Function to calculate ` `// total path ` `int` `totalPath(Node* node) ` `{ ` `    ``static` `int` `count = 0; ` `    ``if` `(node == NULL) { ` `        ``return` `count; ` `    ``} ` `    ``if` `(node->left == NULL ` `        ``&& node->right == NULL) { ` `        ``return` `count + 1; ` `    ``} ` `    ``count = totalPath(node->left); ` `    ``return` `totalPath(node->right); ` `} ` ` `  `// Function for Clockwise Spiral Traversal ` `// of Binary Tree ` `void` `traverse_matrix(``int``** mtrx, ` `                     ``int` `height, ` `                     ``int` `width) ` `{ ` `    ``int` `j = 0, k1 = 0, k2 = 0; ` `    ``int` `k3 = height - 1; ` `    ``int` `k4 = width - 1; ` ` `  `    ``for` `(``int` `round = 0; round < height/2; round++) ` `    ``{ ` `        ``for` `(j = k2; j < width; j++) { ` ` `  `            ``// Only print those values which ` `            ``// are not MAX_INTEGER ` `            ``if` `(mtrx[k1][j] != INT_MAX) { ` `                ``cout << mtrx[k1][j] << ``" "``; ` `            ``} ` `        ``} ` `        ``cout << endl; ` ` `  `        ``k2 = 0; ` `        ``k1++; ` ` `  `        ``for` `(j = k4; j >= 0; j--) { ` ` `  `            ``// Only print those values which ` `            ``// are not MAX_INTEGER ` `            ``if` `(mtrx[k3][j] != INT_MAX) { ` `                ``cout << mtrx[k3][j] << ``" "``; ` `            ``} ` `        ``} ` `        ``cout << endl; ` ` `  `        ``k4 = width - 1; ` `        ``k3--; ` `    ``} ` ` `  `    ``// Condition (one row may be left ` `    ``// traversing) ` `    ``// If number of rows in matrix are odd ` `    ``if` `(height % 2 != 0) { ` `        ``for` `(j = k2; j < width; j++) { ` ` `  `            ``// Only print those values which are ` `            ``// not MAX_INTEGER ` `            ``if` `(mtrx[k1][j] != INT_MAX) { ` `                ``cout << mtrx[k1][j] << ``" "``; ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// Function to print all the paths ` `// in Boundary Root to Leaf  ` `// Path Traversal ` `void` `PrintPath(Node* node) ` `{ ` `    ``// Calculate the length of ` `    ``// longest path of the tree ` `    ``int` `max_len = lengthOfLongestPath(node); ` `     `  `    ``// Calculate total path ` `    ``int` `total_path = totalPath(node); ` `     `  `    ``// Array to store path ` `    ``int``* path = ``new` `int``[max_len]; ` `    ``memset``(path, 0, ``sizeof``(path)); ` ` `  `    ``// Use double pointer to create  ` `    ``// 2D array which will contain ` `    ``// all path of the tree ` `    ``int``** mtrx = ``new` `int``*[total_path]; ` ` `  `    ``// Initialize width for each row  ` `    ``// of matrix ` `    ``for` `(``int` `i = 0; i < total_path; i++) ` `    ``{ ` `        ``mtrx[i] = ``new` `int``[max_len]; ` `    ``} ` ` `  `    ``// Initialize complete matrix with ` `    ``// MAX INTEGER(purpose garbage) ` `    ``for` `(``int` `i = 0; i < total_path; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0; j < max_len; j++) ` `        ``{ ` `            ``mtrx[i][j] = INT_MAX; ` `        ``} ` `    ``} ` ` `  `    ``int` `row = -1; ` `    ``storePath(node, path, 0, mtrx, row); ` ` `  `    ``// Print the circular clockwise spiral ` `    ``// traversal of the tree ` `    ``traverse_matrix(mtrx, total_path, ` `                    ``max_len); ` ` `  `    ``// Release extra memory ` `    ``// allocated for matrix ` `    ``free``(mtrx); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``/*      10   ` `           ``/  \   ` `         ``13    11   ` `              ``/  \   ` `            ``19    23   ` `           ``/ \    / \   ` `          ``21 29 43  15  ` `                   ``/  ` `                  ``7 */` ` `  `    ``// Create Binary Tree as shown ` ` `  `    ``Node* root = newNode(10); ` `    ``root->left = newNode(13); ` `    ``root->right = newNode(11); ` ` `  `    ``root->right->left = newNode(19); ` `    ``root->right->right = newNode(23); ` ` `  `    ``root->right->left->left = newNode(21); ` `    ``root->right->left->right = newNode(29); ` `    ``root->right->right->left = newNode(43); ` `    ``root->right->right->right = newNode(15); ` `    ``root->right->right->right->left = newNode(7); ` ` `  `    ``// Function Call ` `    ``PrintPath(root); ` `    ``return` `0; ` `} `

Output:

```10 13
7 15 23 11 10
10 11 19 21
43 23 11 10
10 11 19 29
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.