Day 30 Leetcode: Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree

# 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 isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
        if len(arr)>0 and root is None:
            return False
        if root.val!=arr[0]:
            return False
        arr=arr[1:]
        if len(arr)==0:
            if root.left==None and root.right==None:
                return True
            else:
                return False
        
        return self.isValidSequence(root.left,arr) or self.isValidSequence(root.right,arr)
        

 

你可能感兴趣的:(Python,编程笔试,Leetcode)