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.
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
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:
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.
- Find the maximum sum leaf to root path in a Binary Tree
- Print the first shortest root to leaf path in a Binary Tree
- GCD from root to leaf path in an N-ary tree
- Boundary Traversal of binary tree
- Print the longest leaf to leaf path in a Binary tree
- Boundary Level order traversal of a Binary Tree
- Iterative Boundary Traversal of Complete Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Root to leaf paths having equal lengths in a Binary Tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Create a binary tree from post order traversal and leaf node array
- Print path from root to a given node in a binary tree
- Sort the path from root to a given node in a Binary Tree
- Print path from root to all nodes in a Complete Binary Tree
- Check if there is a root to leaf path with given sequence
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.