Given a binary tree, the task is to print all the palindromic paths of this binary tree.
Palindromic path: Path in which concatenation of data from root to leaf is the same as the leaf to root, such as 1->2->2->1.
Input: 1 / \ 2 3 / / \ 1 6 3 \ / 2 1 Output: 1, 2, 1 1, 3, 3, 1 Explanation: In above binary tree paths 1, 2, 1 and 1, 3, 3, 1 are palindromic paths because traversal of these paths are the same as root to leaf and leaf to root Input: 7 / \ 4 3 / \ \ 3 6 4 / \ \ / 1 4 4 1 / 7 Output: 7, 4, 6, 4, 7 Explanation: In the above binary tree, there is only one path which is same as root to leaf and leaf to root. that is 7->4->6->4->7
Approach : The idea is to use pre-order traversal to traverse the tree and keep track of the path. Whenever a leaf node is reached then check that the current path is palindromic path or not. Similarly, Recursively Traverse the other sibiling nodes of the tree by backtracking the path of the tree. Following are the steps:
- Start the Pre-order traversal of the tree with the root node.
- Check that current node is not null, if yes then return.
if (node == Null) return;
- Check that the current node is a leaf node or not, if yes then check that the current path is a palindromic path or not.
if (node->left == Null && node->right == Null) isPalindromic(Path);
- Otherwise, If the current node is not leaf node then recursively traverse the left and right child nodes of the current node.
- To check that a path is palindromic or not, Concatenate the data of the nodes from root to leaf and then check that the formed string is a palindrome or not.
Below is the implementation of the above approach:
2, 8, 8, 2, 2, 8, 5, 8, 2,
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.
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all k-sum paths in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print all root-to-leaf paths
- Print all Palindromic Levels Of a Binary Tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Count all k-sum paths in a Binary Tree
- Count even paths in Binary Tree
- Count of exponential paths in a Binary Tree
- Count of Fibonacci paths in a Binary tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Root to leaf paths having equal lengths in a Binary Tree
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print Binary Tree in 2-Dimensions
- Print Right View of a Binary Tree
- Print all K-sum levels in a Binary Tree
- Print the nodes having exactly one child in a Binary tree
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.