Count of root to leaf paths whose permutation is palindrome in a Binary Tree

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     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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

2

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.