# 72. 树的子结构

java

```java
import leetcode.common.TreeNode;
import leetcode.common.TreeUtils;

/**
 * 树的子结构
 *
 * 输入两棵二叉树A，B，判断B是不是A的子结构。（ps：我们约定空树不是任意一个树的子结构）
 */
public class HasSubtreeSolution {
    public boolean HasSubtree(TreeNode root1, TreeNode root2) {
        if(root1==null||root2==null) return false;
        return isSubtree(root1,root2)||HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);
    }

    public boolean isSubtree(TreeNode root1,TreeNode root2){
        if(root2==null) return true;
        if(root1==null) return false;
        return root1.val==root2.val&&isSubtree(root1.left,root2.left)&&isSubtree(root1.right,root2.right);
    }

    public static void main(String[] args) {
        String treeStr1 = "889##24##7##7##";
        String treeStr2 = "89##2##";
        TreeNode root1=TreeUtils.stringToTree(treeStr1);
        TreeNode root2=TreeUtils.stringToTree(treeStr2);
        boolean r=new HasSubtreeSolution().HasSubtree(root1,root2);
        System.out.println(r);
    }
}
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/72-shu-de-zi-jie-gou.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.
