Watch 10 video solutions for Pseudo-Palindromic Paths in a Binary Tree, a medium level problem involving Bit Manipulation, Tree, Depth-First Search. This walkthrough by NeetCodeIO has 17,123 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 1:

Input: root = [2,3,1,3,1,null,1] Output: 2 Explanation: 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:

Input: root = [2,1,1,1,3,null,null,null,null,null,1] Output: 1 Explanation: 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).
Example 3:
Input: root = [9] Output: 1
Constraints:
[1, 105].1 <= Node.val <= 9Problem Overview: You traverse a binary tree from the root to every leaf. Each path produces a sequence of digits (1–9). A path is pseudo‑palindromic if the digits can be rearranged to form a palindrome. The task is to count how many root‑to‑leaf paths satisfy this condition.
Approach 1: Depth-First Search with Frequency Counting (O(n) time, O(h) space)
Run a DFS from the root while maintaining a frequency map of digits seen along the current path. When you reach a leaf, check whether the frequency counts can form a palindrome. A sequence forms a palindrome if at most one digit has an odd count. This check requires iterating through the digit counts and verifying the odd frequency condition. DFS naturally fits the problem because each recursive call represents extending a root‑to‑leaf path. The algorithm visits each node once, giving O(n) time complexity and O(h) space for recursion depth where h is the tree height.
This approach demonstrates the core idea clearly and works well in interviews. It relies on tree traversal using depth-first search and path tracking within a binary tree.
Approach 2: Depth-First Search with Path Count Array (O(n) time, O(h) space)
Instead of a map, maintain a fixed array of size 10 to store counts for digits 1–9. When visiting a node, increment its digit count in the array. When backtracking, decrement it so the parent path remains correct. At each leaf, scan the array and count how many digits have odd frequencies. If the number of odd counts is ≤ 1, the path is pseudo‑palindromic and contributes to the answer. Using a fixed array avoids hash overhead and makes updates constant time.
This version is slightly more efficient and commonly used in competitive programming. The logic still relies on recursive traversal using DFS. With n nodes, each visited once, the time complexity stays O(n). The recursion stack still requires O(h) space.
Recommended for interviews: DFS with a count array is typically preferred. It shows that you recognize the palindrome property (≤1 odd frequency) and optimize storage with a fixed-size structure. The frequency-map approach is still valuable as a stepping stone and clearly communicates the reasoning behind pseudo‑palindromic validation. Strong candidates sometimes further optimize the parity check using bit manipulation, but the DFS counting strategy already meets optimal O(n) performance.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS with Frequency Map | O(n) | O(h) | Clear conceptual solution when explaining palindrome frequency logic |
| DFS with Path Count Array | O(n) | O(h) | Preferred in interviews and contests due to lower overhead and constant-size storage |