LeetCode 572

Subtree of Another Tree

Concepts:

Tree

Description: Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.

Optimal Approach

Simple recursion, for each node in the main tree, check if it is equal to the subtree's root. If it is, recursively check if the subtrees are equal. If not, recursively check the left and right nodes of the main tree node.

Time: O(nk)

Space: O(1), Recursive stack space: O(n + k)

All Problems