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.

**Examples:**

Input:2 / \ 3 1 / \ \ 3 4 2 / \ / \ 2 1 2 1Output:2Explanation:Paths whose one of the permuation are palindrome are - 2 => 3 => 3 => 2 and 2 => 1 => 2 => 1Input:2 / \ a 3 / \ 2 aOutput:2Explanation: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:

## C++

`// C++ implementation to count of ` `// the path whose permutation is ` `// a palindromic path ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long ` ` ` `// Map to store the frequency ` `map<` `char` `, ` `int` `> freq; ` `int` `ans = 0; ` ` ` `// Structure of the node ` `struct` `Node { ` ` ` `char` `val; ` ` ` `struct` `Node *left, *right; ` `}; ` ` ` `// Function to add new node ` `Node* newNode(` `char` `key) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->val = key; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `(temp); ` `} ` ` ` `// Function to check that the path ` `// is a palindrome or not ` `int` `checkPalin() ` `{ ` ` ` `int` `oddCount = 0; ` ` ` `for` `(` `auto` `x : freq) { ` ` ` `if` `(x.second % 2 == 1) ` ` ` `oddCount++; ` ` ` `} ` ` ` `return` `oddCount <= 1; ` `} ` ` ` `// Function to count the root to ` `// leaf path whose permutation is ` `// a palindromic path ` `void` `cntpalin(Node* root) ` `{ ` ` ` `if` `(root == NULL) ` ` ` `return` `; ` ` ` `freq[root->val]++; ` ` ` ` ` `if` `(root->left == NULL ` ` ` `&& root->right == NULL) { ` ` ` ` ` `if` `(checkPalin() == ` `true` `) ` ` ` `ans++; ` ` ` `} ` ` ` `cntpalin(root->left); ` ` ` `cntpalin(root->right); ` ` ` `freq[root->val]--; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `Node* root = newNode(` `'2'` `); ` ` ` `root->left = newNode(` `'a'` `); ` ` ` `root->left->right = newNode(` `'a'` `); ` ` ` `root->left->left = newNode(` `'2'` `); ` ` ` `root->left->right->right = newNode(` `'2'` `); ` ` ` `root->right = newNode(` `'3'` `); ` ` ` ` ` `// Function Call ` ` ` `cntpalin(root); ` ` ` ` ` `cout << ans << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count of root to leaf paths in a Binary Tree that form an AP
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Given a binary tree, print all root-to-leaf paths
- Root to leaf paths having equal lengths in a Binary Tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Maximize count of set bits in a root to leaf path in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Find maximum GCD value from root to leaf in a Binary tree
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Find all root to leaf path sum of a Binary Tree
- Boundary Root to Leaf Path traversal of 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 the maximum sum leaf to root path in a Binary Tree
- Count of leaf nodes of the tree whose weighted string is a palindrome
- Print root to leaf paths without using recursion
- Sum of all the numbers that are formed from root to leaf paths
- Remove nodes on root to leaf paths of length < K
- Print all root to leaf paths with there relative positions
- Count even paths in 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 contribute@geeksforgeeks.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.