Easy · Trees
Subtree of Another Tree
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
Core interview problems are free with a full runner and lessons; Pro opens the complete library.