Same Tree

Binary Tree
Easy

Approach

Diagrams

Analysis

Time

O(n)

Space

O(n)
from collections import deque


class Solution:
    def isSameTree(self, root, otherRoot):
        queue = deque([(root, otherRoot)])

        while queue:
            node, otherNode = queue.popleft()

            if not (node or otherNode):
                continue

            if node is None or otherNode is None:
                return False

            if node.val != otherNode.val:
                return False

            queue.append((node.left, otherNode.left))
            queue.append((node.right, otherNode.right))

        return True

Learn

#Breadth-First Search

Videos