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.
graph TD
A(2)
B(3)
C(1)
D(3)
E(1)
F(1)
A --- B
A --- C
B --- D
B --- E
C ~~~ N1:::hidden
C --- F
classDef hidden display:none
linkStyle 0 stroke:red,stroke-width:4px
linkStyle 2 stroke:red,stroke-width:4px
linkStyle 1 stroke:lightgreen,stroke-width:4px
linkStyle 5 stroke:lightgreen,stroke-width:4px
1
2
3
Input: root =[2,3,1,3,1,null,1]Output: 2Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in[3,2,3](palindrome) and the green path [2,1,1] can be rearranged in[1,2,1](palindrome).
Example 2:
graph TD
A(2)
B(1)
C(1)
D(1)
E(3)
F(1)
A --- B & C
B --- D & E
E ~~~ N1:::hidden
E --- F
classDef hidden display:none
linkStyle 0 stroke:lightgreen,stroke-width:4px
linkStyle 2 stroke:lightgreen,stroke-width:4px
1
2
3
Input: root =[2,1,1,1,3,null,null,null,null,null,1]Output: 1Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in[1,2,1](palindrome).
If node val exists in set, remove it; else add it. Consider the path: 1→1, then we have set size of 0, as even nodes cancel each other out. If it is 1→1→1 then we have just value remaining in set.
At the leaf node, check if the size of set <= 1. If the set size is less than 1, then we have found 1 palindromic path
classSolution {
publicintpseudoPalindromicPaths(TreeNode root) {
return dfs(root, new HashSet<>());
}
privateintdfs(TreeNode curr, Set<Integer> numSet) {
if (curr ==null) return 0;
// Toggle current node value in the setif (numSet.contains(curr.val)) {
numSet.remove(curr.val);
} else {
numSet.add(curr.val);
}
// If it's a leaf node, check for pseudo-palindrome conditionif (curr.left==null&& curr.right==null) {
return numSet.size() <= 1 ? 1 : 0;
}
// Continue DFS for both left and right subtreesint leftPaths = dfs(curr.left, new HashSet<>(numSet));
int rightPaths = dfs(curr.right, new HashSet<>(numSet));
return leftPaths + rightPaths;
}
}
To determine the number of pseudo-palindromic paths, we need to check for paths where the node values can form at least one permutation that is a palindrome. For a sequence to potentially form a palindrome:
At most one digit can have an odd count while all others must have even counts.
Here is the approach:
Binary Tree Traversal: Perform a Depth-First Search (DFS) traversal from the root to leaf nodes.
Frequency Count via Mask: Use a bitmask to track the count of digits. Each bit position corresponds to the digits 1 to 9. Flip the bits as you encounter digits in the path.
Check Palindromic Condition: At a leaf node, the path is pseudo-palindromic if the bitmask has at most one bit set (x & (x - 1) == 0).
Return Count: Increment the counter for pseudo-palindromic paths.