#572

Subtree of Another Tree

pupil · 435 · lc easy +25 · verified · 51.3% accepted · 8,872 likes · top 40%

play →

Description

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

Code

1
2
3
4
5
6
7
8
9