<p>Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be <strong>pseudo-palindromic</strong> if at least one permutation of the node values in the path is a palindrome.</p>

<p><em>Return the number of <strong>pseudo-palindromic</strong> paths going from the root node to leaf nodes.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/05/06/palindromic_paths_1.png" style="width: 300px; height: 201px;" /></p>

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

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/05/07/palindromic_paths_2.png" style="width: 300px; height: 314px;" /></strong></p>

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

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = [9]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 9</code></li>
</ul>

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int:

        # 如果输入的节点为空节点，则返回 0
        if not root:
            return 0
        
        # 初始化一个字典，用于保存存储在节点堆栈中的节点的变量存储情况。
        # 如果变量出现过偶数次，该变量对应的值为 False ；如果出现过奇数次，则为 True 。
        stack_vals = {}

        # 记录伪回文数列的数量（结果的数量）。一个伪回文数列的特征是：数列里至多存在一个出现
        # 次数为奇数的元素。
        result_count = 0

        def search_tree(node):
            """使用深度优先搜索算法对树进行遍历"""

            nonlocal stack_vals, result_count

            # 将该节点的值记录到 stack_vals 中
            if node.val not in stack_vals:
                stack_vals[node.val] = False
            stack_vals[node.val] = not stack_vals[node.val]

            # 如果该节点是叶子节点，则判断从根节点到叶子节点的序列是不是伪回文序列
            if not node.left and not node.right:
                # 统计从根节点到叶子节点中出现次数为奇数的元素的数量
                trues = 0
                for val, val_count in stack_vals.items():
                    if val_count:
                        trues += 1
                # 如果该数量为零或者一，代表这是一个伪回文数列
                if trues <= 1:
                    result_count += 1

            # 如果这个节点有左孩子，则转去遍历左孩子
            if node.left:
                search_tree(node.left)

            # 如果这个节点有右孩子，则遍历查看右孩子
            if node.right:
                search_tree(node.right)

            # 将该节点的值从 stack_vals 移除
            stack_vals[node.val] = not stack_vals[node.val]

        # 开始遍历整棵树
        search_tree(root)

        return result_count



42700