Same Tree

Given the roots of two binary trees, write a function to check if they are the same. Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Constraints

  • The number of nodes in both trees is in the range [0, 100].
  • -104 <= Node.val <= 104
Saving… · 9 lines
# 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(self, p: TreeNode, q: TreeNode) -> bool:
Console

Click Run to execute sample tests.