Skip to main content

1457 - Pseudo-Palindromic Paths in a Binary Tree

Details

KeyValue
Linkhttps://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
LanguagePython 3
Runtime1860 ms, faster than 15.58% of Python3 online submissions for Pseudo-Palindromic Paths in a Binary Tree
Memory Usage85.1 MB, less than 71.65% of Python3 online submissions for Pseudo-Palindromic Paths in a Binary Tree
DatastructuresTreeNode
AlgorithmsBacktracking
ComplexityTime: O(N) Memory: O(K+H)

Procedure

  1. ...

Code

# 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(node, cur):
if not node: return 0
result = 0

if node.val in cur: cur.remove(node.val)
else: cur.add(node.val)

if not node.left and not node.right:
if len(cur) <= 1: result = 1
else:
result = result + dfs(node.left, cur) + dfs(node.right, cur)

if node.val in cur: cur.remove(node.val)
else: cur.add(node.val)

return result

return dfs(root, set())