> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Pseudo-Palindromic Paths in a Binary Tree

> Tested Python solution for LeetCode 1457 with 28 pytest cases. Generate a practice environment with lcpy.

LeetCode 1457, [Medium](/catalog/medium). Topics: [Bit Manipulation](/catalog/topics/bit-manipulation), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/description/).

Generate this problem as a practice environment: tested reference solution, 28 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1457   # by problem number
lcpy gen -s pseudo_palindromic_paths_in_a_binary_tree   # by problem name
```

## Problem

Given a binary tree where node values are digits from 1 to 9 only. 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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/05/06/palindromic_paths_1.png)

```
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 the red path and the 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](https://assets.leetcode.com/uploads/2020/05/07/palindromic_paths_2.png)

```
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).
```

```
Input: root = [9]
Output: 1
```

### Constraints

* The number of nodes in the tree is in the range \[1, 10^5]
* 1 \<= Node.val \<= 9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/pseudo_palindromic_paths_in_a_binary_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/pseudo_palindromic_paths_in_a_binary_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h)
    def pseudo_palindromic_paths(self, root: TreeNode[int] | None) -> int:
        if root is None:
            return 0
        total = 0
        stack: list[tuple[TreeNode[int], int]] = [(root, 1 << root.val)]
        while stack:
            node, mask = stack.pop()
            if node.left is None and node.right is None:
                if mask & (mask - 1) == 0:
                    total += 1
                continue
            if node.left is not None:
                stack.append((node.left, mask ^ (1 << node.left.val)))
            if node.right is not None:
                stack.append((node.right, mask ^ (1 << node.right.val)))
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.