Given a Binary Tree, the task is to print all palindromic levels of this tree.
Palindrom Level Any level of a Binary tree is said to be a palindromic level if on traversing it from left to right, the result is same as traversing that level from right to left.
Input: 1 / \ 12 13 / / \ 11 6 11 \ / 2 2 Output: 1 11 6 11 2 2 Explanation: First, third and fourth levels are plaindrome. Input: 7 / \ 22 22 / \ \ 3 6 3 / \ \ / 1 5 8 1 / 23 Output: 7 22 22 3 6 3 23 Explanation: First, second, third and fifth levels are plaindrome.
Approach: In order to check if each level is a Palindromic level, we need to first do the Level Order Traversal of the Binary tree to get the values at each level. Then for each level, check if it a palindrome or not.
Here a Queue data structure is used to store the levels of the Tree while doing the Level Order Traversal.
Below is the implementation of the above approach:
10 13 13 21 22 22 21 8
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 Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all K-sum levels in a Binary Tree
- Print all Co-Prime Levels of a Binary Tree
- Print all Exponential Levels of a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print all Prime Levels of a Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print Binary Tree levels in sorted order
- Print even positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print Palindromic Paths of Binary tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Print the nodes at odd levels of a tree
- Averages of Levels in Binary Tree
- Left-Right traversal of all the levels of Binary tree
- Reverse alternate levels of a perfect binary tree
- Number of edges in a perfect binary tree with N levels
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.