<filename>recursion/100. Same Tree_medium.py
'''
Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
'''


# 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 isSameTree_recur(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:

        if not p and not q:
            return True
        if not p or not q:
            return False
        if p.val != q.val:
            return False

        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

    def isSameTree(self, p, q):
        def check_same(p, q):
            if not p and not q:
                return True
            if not p or not q:
                return False
            if p.val != q.val:
                return False

            return True

        que = []
        que.append((p, q))
        while que:
            p, q = que.pop(0)
            if not check_same(p, q):
                return False
            if p and q:
                que.append((p.left, q.left))
                que.append((p.right, q.right))
        return True
<filename>recursion/100. Same Tree_medium.py
'''
Given the roots of two binary trees p and q, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
'''


# 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 isSameTree_recur(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:

        if not p and not q:
            return True
        if not p or not q:
            return False
        if p.val != q.val:
            return False

        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

    def isSameTree(self, p, q):
        def check_same(p, q):
            if not p and not q:
                return True
            if not p or not q:
                return False
            if p.val != q.val:
                return False

            return True

        que = []
        que.append((p, q))
        while que:
            p, q = que.pop(0)
            if not check_same(p, q):
                return False
            if p and q:
                que.append((p.left, q.left))
                que.append((p.right, q.right))
        return True
Given the roots of two binary trees p and q, write a function to check if they are the same or not.  Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. # 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