-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1457.二叉树中的伪回文路径.py
49 lines (42 loc) · 1.13 KB
/
1457.二叉树中的伪回文路径.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#
# @lc app=leetcode.cn id=1457 lang=python
#
# [1457] 二叉树中的伪回文路径
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def pseudoPalindromicPaths (self, root):
"""
:type root: TreeNode
:rtype: int
"""
stack = []
ans = self.dfs(root, stack)
return ans
def dfs(self, root, stack):
if root == None:
return 0
ans = 0
if root.val in stack:
stack.remove(root.val)
else:
stack.append(root.val)
if root.left == None and root.right == None:
if len(stack) <= 1:
ans += 1
if root.left != None:
ans += self.dfs(root.left, stack)
if root.right != None:
ans += self.dfs(root.right, stack)
if root.val in stack:
stack.remove(root.val)
else:
stack.append(root.val)
return ans
# @lc code=end