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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Print the longest leaf to leaf path in a Binary tree
- Find the maximum sum leaf to root path in a Binary Tree
- Maximize count of set bits in a root to leaf path in a binary tree
- Print the first shortest root to leaf path in a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Find all root to leaf path sum of a Binary Tree
- Boundary Traversal of binary tree
- Iterative Boundary Traversal of Complete Binary tree
- Boundary Level order traversal of a Binary Tree
- GCD from root to leaf path in an N-ary tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Count of root to leaf paths in a Binary Tree that form an AP
- Find maximum GCD value from root to leaf in a Binary tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count root to leaf paths having exactly K distinct nodes in a Binary Tree
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : sanjeev2552