PIXELBANKv8.2.1
Menu

Subtree of Another Tree

EasyTrees

Given two binary trees root and subRoot (as level-order arrays), return True if subRoot is a subtree of root.

A subtree is a node and all its descendants matching exactly.

Example:

Input:
3,4,5,1,2
4,1,2
Output:
True
Reasoning:
  • The binary tree root is constructed from the level-order array: 3,4,5,1,2, resulting in a tree with 3 as the root, 4 and 5 as its children, and 1 and 2 as children of 4.
  • The binary tree subRoot is constructed from the level-order array: 4,1,2, resulting in a tree with 4 as the root and 1 and 2 as its children.
  • We then check if subRoot is a subtree of root by comparing the structure and node values of subRoot with the corresponding subtree of root rooted at node 4.
  • Since the subtree of root rooted at node 4 has the same structure and node values as subRoot, we conclude that subRoot is a subtree of root, resulting in an output of True.

Constraints:

  • 1 <= nodes in root <= 2000
  • 1 <= nodes in subRoot <= 1000
Editor

Test Results

0/0
Run code to see test results.