[LeetCode][python3]Day30. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree (30-Day LeetCoding Challenge)

class Solution:
def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
if not root and arr:
return False
buffer1=[root]
buffer2=[]
f=lambda x:x.val if x else None
for item in arr[:-1]:
buffer2=[x for x in buffer1 if f(x)==item]
if not buffer2:
return False
buffer1=[]
for x in buffer2:
buffer1.append(x.left)
buffer1.append(x.right)
for x in buffer1:
if x and x.val==arr[-1]:
if x.left==None and x.right==None:
return True
return False

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store