Pseudo-Palindromic Paths in a Binary Tree

Given a binary tree whose node values are digits from `1` to `9`, a root-to-leaf path is **pseudo-palindromic** if its values can be rearranged into a palindrome. Return the number of pseudo-palindromic paths going from the root to a leaf.

30:00

Pseudo-Palindromic Paths in a Binary Tree
medium
Free written solution
Topics
Companies

Given a binary tree whose node values are digits from 1 to 9, a root-to-leaf path is pseudo-palindromic if its values can be rearranged into a palindrome.

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

Example 1:
Input: {"root":[2,3,1,3,1,null,1]}
Output: 2
Constraints:
  • The number of nodes in the tree is in the range [1,105][1, 10^5].

  • 1≤1 \le Node.val ≤9\le 9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[2,3,1,3,1,null,1]}

A multiset of digits can form a palindrome when at most one digit appears an odd number of times. Walk every root-to-leaf path, flipping one parity bit per digit.

palindromic paths 0
leaves checked 0
path: (none)

parity mask (lit = odd count on the current path) → 0 odd

1even
2even
3even
4even
5even
6even
7even
8even
9even
231311
Variables
VariableValue
mask000000000
leaves3
DepthFunction Call
Stack empty
0/7