1457. Pseudo-Palindromic Paths in a Binary Tree

https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/

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:

Example 2:

Example 3:

Constraints:

  • The given binary tree will have between 1 and 10^5 nodes.

  • Node values are digits from 1 to 9.

二叉树的结点值为[0~9],统计能通过permutation变成回文的(从root到叶的)路径数。能通过permutation成为回文需要满足至多有一个元素出现奇数次。由于值的范围是0到9,因此可以利用一个整数cnt和位操作统计出现频率,位的位置代表对应的值频率,当为1时是奇数,偶数时是0,并在对应位做异或。当只有一个1时,cnt和cnt-1相与为0。

Last updated

Was this helpful?