1457. Pseudo-Palindromic Paths in a Binary Tree
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
Python
DFS
class Solution:
def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int:
def dfs(node, path_origin: set):
if not node:
return 0
path = path_origin.copy()
if node.val in path:
path.remove(node.val)
else:
path.add(node.val)
if not node.left and not node.right:
# Current node is leave, check the path
return 1 if len(path) <= 1 else 0
return dfs(node.left, path) + dfs(node.right, path)
return dfs(root, set())