Easy · Trees

Is subtree

Given the roots of two binary trees, root and sub, return true if some node of root has a subtree (the node and all its descendants) identical to sub in shape and values, and false otherwise. An empty sub is contained in every tree.

Examples

Example 1

[3,4,5,1,2] ⊇ [4,1,2]

Output: is subtree

Example 2

[3,4,5,1,2,null,null,0] ⊉ [4,1,2]

Output: not a subtree

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems