Subtree of Another Tree - aloalgo

Subtree of Another Tree

Medium

You are given the roots of two binary trees, root and subRoot.

Your task is to determine if there is a subtree of root that is identical to subRoot.

You may assume that:

  • A subtree of a binary tree consists of a node in the tree and all of its descendants. The subtree can also be the tree root itself.

Return true if subRoot is a subtree of root, otherwise return false.

Example 1

Inputs
1245367
367
Output
True
Explanation:

subRoot with value 3 exists as a subtree in root.

Example 2

Inputs
1245367
458
Output
False
Explanation:

subRoot with value 8 does not exist in root.

Example 3

Inputs
123
123
Output
True
Explanation:

subRoot is identical to root, so it is a subtree.

Loading...
Inputs
1245367
367
Output
True

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!