1457. 二叉树中的伪回文路径
题目描述
解题思路
1、二叉树前序遍历
2、位运算
代码实现
class Solution:
def pseudoPalindromicPaths (self, root: TreeNode) -> int:
if not root:
return 0
self.res = 0
def pre_order(root: TreeNode, num: int) -> None:
if not root:
return
num = num ^ (1 << root.val)
if not root.left and not root.right:
if num == 0 or (num & (num - 1)) == 0:
self.res += 1
pre_order(root.left, num)
pre_order(root.right, num)
pre_order(root, 0)
return self.res