Advertisement

Subtree of Another Tree - LeetCode 572 Solution

Subtree of Another Tree - Complete Solution Guide

Subtree of Another Tree is LeetCode problem 572, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself. Example 1: Input: root = [3,4,5,1,2], subRoot = [4,1,2] Output: true Example 2: Input: root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2] Output: fal

Detailed Explanation

The problem asks us to determine if a binary tree `subRoot` is a subtree of another binary tree `root`. A subtree is defined as a node in `root` and all of its descendants. Crucially, it must have the exact same structure and node values. The input consists of the root nodes of two binary trees, `root` and `subRoot`. The output should be a boolean value: `true` if `subRoot` is a subtree of `root`, and `false` otherwise. The constraints specify the size and value ranges for the nodes in both trees.

Solution Approach

The solution uses a recursive approach. First, it checks if the `subRoot` is `null`. If it is, it returns `true`. If the `root` is `null` and `subRoot` is not, it returns `false`. Then, it uses a helper function `isSameTree` (or `isSame`) to check if the subtree rooted at the current node of `root` is identical to `subRoot`. If they are identical, it returns `true`. Otherwise, it recursively checks if `subRoot` is a subtree of the left or right subtree of `root`. This effectively explores all possible starting points in `root` for a potential `subRoot` match.

Step-by-Step Algorithm

  1. Step 1: Check if `subRoot` is `null`. If so, return `true` (as an empty tree is a subtree of anything).
  2. Step 2: Check if `root` is `null`. If so, return `false` (as a non-empty tree cannot be a subtree of an empty tree).
  3. Step 3: Call the `isSameTree` (or `isSame`) helper function to check if the `root` tree and `subRoot` tree are identical. If they are, return `true`.
  4. Step 4: If the trees are not identical, recursively call `isSubtree` on the left subtree of `root` and `subRoot`.
  5. Step 5: If Step 4 returns `false`, recursively call `isSubtree` on the right subtree of `root` and `subRoot`.
  6. Step 6: Return the result of the logical OR operation between the results of Step 4 and Step 5. This is because `subRoot` only needs to exist as a subtree in either the left or right subtrees of `root`.

Key Insights

  • Insight 1: The core idea is to recursively traverse the `root` tree and, for each node, check if the subtree rooted at that node is identical to `subRoot`.
  • Insight 2: We need a separate function to determine if two trees are identical. This function should recursively compare the structure and node values of the two trees.
  • Insight 3: The `subRoot` can be considered as a subtree of itself, and an empty `subRoot` tree must always return `true` since an empty tree is a subtree of any other tree.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(n)

Topics

This problem involves: Tree, Depth-First Search, String Matching, Binary Tree, Hash Function.

Companies

Asked at: Compass, Jump Trading, Morgan Stanley, eBay.