Pseudo-Palindromic Paths in a Binary Tree

Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.

Return the number of pseudo-palindromic paths going from the root node to leaf nodes.

Example:

Binary Tree

Now there are 3 paths in the given binary tree.

2 -> 3 -> 3
2 -> 3 -> 1
2 -> 4 -> 2

Out of these 2 -> 4 -> 2 is alreday a palindrome and 2 -> 3 -> 3 is a pseudo palindromic path, because one permutation of the path 3 -> 2 -> 3 is a palindrome. Where no permutation of 2 -> 3 -> 1 is a palindrome.

This problem is an extension of Palindrome Permutation.

Where we wrote an efficient algorithm that checks whether any permutation of an input string is a palindrome. So, here we need to find all the paths of a binary tree (A path is defined by a String having values from the root to a leaf node) and then need to check that if the path is a pseudo palindromic path.

Here we will create a List<Map<Integer,Integer>> where each map contains the frequency of characters in that path. We will use a DFS and Backtracking mechanism to calculate the paths. Then we need to check how many maps are pseudo palindromic by applying the same logic of Palindrome Permutation.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {
    public int pseudoPalindromicPaths(TreeNode root) {
        if (root == null) {
            return 0;
        }
        List<Map<Integer, Integer>> pathList = new ArrayList<>();
        List<Integer> curPath = new ArrayList<>();
        Map<Integer, Integer> curFrequencyMap = new HashMap<>();
        DFS(root, curFrequencyMap, curPath, pathList);
        int count = 0;
        for (Map<Integer, Integer> curMap : pathList) {
            if (isPseudoPalindromicPath(curMap)) {
                count++;
            }
        }
        return count;
    }

    private void DFS(TreeNode node, Map<Integer, Integer> curFrequencyMap,
                     List<Integer> curPath, List<Map<Integer, Integer>> pathList) {
        if (node == null) {
            return;
        }
        int cur_val = node.val;
        int freq = curFrequencyMap.getOrDefault(cur_val, 0);
        curFrequencyMap.put(cur_val, freq + 1);
        curPath.add(cur_val);
        if (node.left == null && node.right == null) {
            pathList.add(new HashMap<>(curFrequencyMap));
        }
        DFS(node.left, curFrequencyMap, curPath, pathList);
        DFS(node.right, curFrequencyMap, curPath, pathList);

        int size = curPath.size();
        int last_val = curPath.get(size - 1);
        freq = curFrequencyMap.get(last_val);
        if (freq == 1) {
            curFrequencyMap.remove(last_val);
        } else {
            curFrequencyMap.put(last_val, freq - 1);
        }
        curPath.remove(size - 1);
    }

    private boolean isPseudoPalindromicPath(Map<Integer, Integer> curMap) {
        int odd_count = 0;
        for (int key : curMap.keySet()) {
            int cur_freq = curMap.get(key);
            if (cur_freq % 2 == 0) {
                continue;
            } else {
                odd_count++;
            }
            if (odd_count > 1) {
                return false;
            }
        }
        return odd_count == 0 || odd_count == 1;
    }
}

Categories: Binary Tree

Tagged as:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s