Given a binary tree where node contains characters, the task is to count the number of paths from root vertex to leaf such that at least one permutation of the node values in the path is a palindrome.
Input: 2 / \ 3 1 / \ \ 3 4 2 / \ / \ 2 1 2 1 Output: 2 Explanation: Paths whose one of the permuation are palindrome are - 2 => 3 => 3 => 2 and 2 => 1 => 2 => 1 Input: 2 / \ a 3 / \ 2 a Output: 2 Explanation: Palindromic paths are 2 => a => 2 and 2 => a => a
Approach: The idea is to use pre-order traversal to traverse the binary tree and keep track of the path. Whenever a leaf node is reached then check that if any permutation of nodes values in the current path is a palindromic path or not.
To check the permutation of the values of the nodes is palindromic or not maintain the frequency of each character using a map. The path will be palindromic if the number of elements with odd frequency is at most 1.
Below is the implementation of the above approach:
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths in a Binary Tree that form an AP
- 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
- Root to leaf paths having equal lengths in a Binary Tree
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Count of leaf nodes of the tree whose weighted string is a palindrome
- Maximize count of set bits in a root to leaf path in a binary tree
- Sum of all the numbers that are formed from root to leaf paths
- Remove nodes on root to leaf paths of length < K
- Print root to leaf paths without using recursion
- Print all root to leaf paths with there relative positions
- 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
- Print all the paths from root, with a specified sum in Binary tree
- Find the maximum sum leaf to root path in a Binary Tree
- Print the first shortest root to leaf path in a Binary Tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Maximum value of Bitwise AND from root to leaf 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 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.