-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1457.py
26 lines (22 loc) · 882 Bytes
/
m1457.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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:
def dfs(curr: Optional[TreeNode], vals: List[bool] = [False] * 10) -> int :
if not curr :
return 0
vals[curr.val] = not vals[curr.val]
if not curr.left and not curr.right :
oddCnt = vals.count(True)
vals[curr.val] = not vals[curr.val]
if oddCnt <= 1 :
return 1
return 0
output = dfs(curr.left) + dfs(curr.right)
vals[curr.val] = not vals[curr.val]
return output
return dfs(root)