Leetcode Problem 2791. Count Paths That Can Form a Palindrome in a Tree
2791. Count Paths That Can Form a Palindrome in a Tree
Leetcode Solutions
DFS + Bitmask + Hash Map
Initialize a hashmap to store the frequency of each bitmask.
Perform a DFS traversal starting from the root node (0), calculating the bitmask for each node.
For each node, increment the count of its bitmask in the hashmap.
For each node, calculate the total count of valid pairs by adding the frequency of the current bitmask and the frequencies of bitmasks that differ by one bit.
Return the total count of valid pairs divided by 2 to account for the condition u < v.