Problem
Ask
Submissions

Problem: Subtree of Another Tree

Medium
30 min
Explore how to verify whether a given binary tree is a subtree of another by comparing their structures and nodes. Understand the problem constraints and implement an efficient solution that runs in optimal time and space. This lesson helps you practice solving tree-based coding challenges with a clear approach to algorithm efficiency and correctness.

Statement

Given the roots of two binary trees, root and sub_root, return TRUE if there is a subtree of root with the same structure and nodes as sub_root. Otherwise, return FALSE.

A tree can only be considered a subtree of the root tree iff all the descendants (children) of the node appear in sub_root.

Constraints:

  • The number of nodes in the root tree is in the range [1,500].[1,500].

  • The number of nodes in the sub_root tree is in the range [1,250].[1,250].

  • 104−10^4 \leq root.data 104\leq 10^4

  • 104−10^4 \leq sub_root.data 104\leq 10^4

Problem
Ask
Submissions

Problem: Subtree of Another Tree

Medium
30 min
Explore how to verify whether a given binary tree is a subtree of another by comparing their structures and nodes. Understand the problem constraints and implement an efficient solution that runs in optimal time and space. This lesson helps you practice solving tree-based coding challenges with a clear approach to algorithm efficiency and correctness.

Statement

Given the roots of two binary trees, root and sub_root, return TRUE if there is a subtree of root with the same structure and nodes as sub_root. Otherwise, return FALSE.

A tree can only be considered a subtree of the root tree iff all the descendants (children) of the node appear in sub_root.

Constraints:

  • The number of nodes in the root tree is in the range [1,500].[1,500].

  • The number of nodes in the sub_root tree is in the range [1,250].[1,250].

  • 104−10^4 \leq root.data 104\leq 10^4

  • 104−10^4 \leq sub_root.data 104\leq 10^4