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

Open every published interview problem, solution, scene, and memory room free, with no account.

More Trees problems