Subtree of Another Tree

Given the roots of two binary trees, return true if there is a subtree of the root with the same structure and node values as subRoot, and false otherwise. A subtree of a binary tree is a tree that consists of a node in the original tree and all of its descendants; the original tree itself is also considered a subtree.

Constraints

  • The number of nodes in the root tree is in the range [1, 2000].
  • The number of nodes in the subRoot tree is in the range [1, 1000].
  • -10000 <= root.val <= 10000
  • -10000 <= subRoot.val <= 10000
Saving… · 10 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 isSubtree(self, root, subRoot) -> bool:
pass
Console

Click Run to execute sample tests.