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

  1. My solution
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

--

--

--

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What I learned from setting up CI for a COVID-19 Exposure Notification Open Source project

Update from Core Team — January 22, 2022

ARTH Task 13

Heroku: Cloud Application Platform

How to set up Samba to share a Ceph filesystem with Active Directory access control

BANCA bi-weekly report 1.2.2019

Data Structures 101: Introduction to Data Structures and Algorithms

Examing the FizzBuzz From a New Perspective

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
N2I

N2I

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

More from Medium

Time Complexity of Python Data Structures

Two Sum | Interview Question

Tree Traversals Iterative & Recursive

Implementing Linked List Operations in Python