"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param s: the s' root
    @param t: the t' root
    @return: whether tree t has exactly the same structure and node values with a subtree of s
    """

    def isSubtree(self, s, t):
        # Write your code here
        if s is None:
            return t == None
        if self.isvalid(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

    def isvalid(self, s, t):
        if s is None and t is None:
            return True
        if s is None or t is None:
            return False
        if s.val != t.val:
            return False
        return self.isvalid(s.left, t.left) and self.isvalid(s.right, t.right)
6363730133963458cf108343d0e3f02c55ac4bdd
<mask token>


class Solution:
    <mask token>
    <mask token>
    <mask token>

<mask token>


class Solution:
    <mask token>

    def isSubtree(self, s, t):
        if s is None:
            return t == None
        if self.isvalid(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
    <mask token>

<mask token>


class Solution:
    <mask token>

    def isSubtree(self, s, t):
        if s is None:
            return t == None
        if self.isvalid(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

    def isvalid(self, s, t):
        if s is None and t is None:
            return True
        if s is None or t is None:
            return False
        if s.val != t.val:
            return False
        return self.isvalid(s.left, t.left) and self.isvalid(s.right, t.right)

<mask token>


class Solution:
    """
    @param s: the s' root
    @param t: the t' root
    @return: whether tree t has exactly the same structure and node values with a subtree of s
    """

    def isSubtree(self, s, t):
        if s is None:
            return t == None
        if self.isvalid(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

    def isvalid(self, s, t):
        if s is None and t is None:
            return True
        if s is None or t is None:
            return False
        if s.val != t.val:
            return False
        return self.isvalid(s.left, t.left) and self.isvalid(s.right, t.right)

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""


class Solution:
    """
    @param s: the s' root
    @param t: the t' root
    @return: whether tree t has exactly the same structure and node values with a subtree of s
    """

    def isSubtree(self, s, t):
        # Write your code here
        if s is None:
            return t == None
        if self.isvalid(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

    def isvalid(self, s, t):
        if s is None and t is None:
            return True
        if s is None or t is None:
            return False
        if s.val != t.val:
            return False
        return self.isvalid(s.left, t.left) and self.isvalid(s.right, t.right)