Easy

Quiz

#572 Subtree of Another Tree

APPROACH

Given the roots of two binary trees root and subRoot, return true if root contains a subtree identical in structure and node values to subRoot, or false otherwise. A subtree consists of a node and all its descendants; a tree is considered a subtree of itself.

Example 1:

Input: root = [3,4,5,1,2], subRoot = [4,1,2]
Output: true

Example 2:

Input: root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
Output: false
1 of 4
1:00

What is the optimal approach for this problem?