# 572. Subtree of Another Tree

### Description

Given two **non-empty** binary trees **s** and **t**, check whether tree **t** has exactly the same structure and node values with a subtree of **s**. A subtree of **s** is a tree consists of a node in **s** and all of this node's descendants. The tree **s** could also be considered as a subtree of itself.

### Constraints

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/subtree-of-another-tree/)
* ProgramCreek
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:**

s = \[3, 4, 5, 1, 2]

<div align="left"><img src="/files/-MUHTvRK88Rtxmg93Xs0" alt=""></div>

t = \[4, 1, 2]

<div align="left"><img src="/files/-MUHU7Ntuf70Hy2n8RyY" alt=""></div>

**Output:** true
{% endtab %}

{% tab title="Example 2" %}
**Input:**

s = \[3, 4, 5, 1, 2, null, null, null, null, 0]

<div align="left"><img src="/files/-MUHUpfTvRgaXWnkXeQN" alt=""></div>

t = \[4, 1, 2]

<div align="left"><img src="/files/-MUHU7Ntuf70Hy2n8RyY" alt=""></div>

**Output:** false
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="TreeNode" %}

```java
// Definition for a binary tree node.
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { 
        this.val = val;
    }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
```

{% endtab %}

{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(m^2 + n^2 + m*n). A total of n nodes of the tree s and m 
 *    nodes of tree t are traversed. Assuming string concatenation takes O(k) 
 *    time for strings of length k and indexOf takes O(m*n).
 * Space complexity : O(max(m,n)). The depth of the recursion tree can go 
 *    upto n for tree t and m for tree s in worst case.
 */

public class Solution {
    HashSet < String > trees = new HashSet < > ();
    public boolean isSubtree(TreeNode s, TreeNode t) {
        String tree1 = preorder(s, true);
        String tree2 = preorder(t, true);
        return tree1.indexOf(tree2) >= 0;
    }
    public String preorder(TreeNode t, boolean left) {
        if (t == null) {
            if (left)
                return "lnull";
            else
                return "rnull";
        }
        return "#"+ t.val + " " + preorder(t.left, true) + " " + 
                preorder(t.right, false);
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : O(m*n). In worst case(skewed tree) traverse function 
 *    takes O(m*n) time.
 * Space complexity : O(n). The depth of the recursion tree can go upto n. 
 *    n refers to the number of nodes in s.
 */
 
class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s == null) {
            return false;
        }
        return isSameTree(s, t) || isSubtree(s.left, t) || 
                isSubtree(s.right, t);
    }
    
    private boolean isSameTree(TreeNode n1, TreeNode n2) {
        if(n1 == null && n2 == null) {
            return true;
        }
        if(n1 == null || n2 == null) {
            return false;
        }
        return n1.val == n2.val && isSameTree(n1.left, n2.left) && 
                isSameTree(n1.right, n2.right);
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code-snippets.hbamithkumara.com/leetcode/problems/501-600/subtree-of-another-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
