# 30. 相同的树

java

```java
// 链表工具类
public class LinkedUtils {
    public static ListNode arrayToLinkedList(int[] array){
        if(array==null) return null;
        //head node 
        ListNode root=new ListNode(-1);
        ListNode p=root;

        int size=array.length;
        int i=0;
        while(i<size){
            ListNode q=new ListNode(array[i++]);
            q.next=null;
            p.next=q;
            p=q;
        }

        return root.next;
    }

    public static void print(ListNode root) {
        if(root==null) System.out.println("root is null");
        else{
            while(root!=null){
                System.out.print(root.val);
                if(root.next!=null){
                    System.out.print("->");
                }
                root=root.next;
            }
            System.out.println();
        }
    }

    public static int length(ListNode root) {
        if(root==null) return 0;
        int n=0;
        while(root!=null){
            root=root.next;
            n++;
        }
        return n;
    }

    // 返回指向链表尾部的指针
    public static ListNode moveToTail(ListNode root) {
        if(root==null) return null;
        ListNode tail=root;
        while(tail.next!=null){
            tail=tail.next;
        }
        return tail;
    }
}


// 定义链表
public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val=val;
    }
}


/**
 * 100. 相同的树
 * 给定两个二叉树，编写一个函数来检验它们是否相同。
 *
 * 如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。
 *
 * 示例 1:
 *
 * 输入:       1         1
 *           / \       / \
 *          2   3     2   3
 *
 *         [1,2,3],   [1,2,3]
 *
 * 输出: true
 *
 * 示例 2:
 *
 * 输入:      1          1
 *           /           \
 *          2             2
 *
 *         [1,2],     [1,null,2]
 *
 * 输出: false
 *
 * 示例 3:
 *
 * 输入:       1         1
 *           / \       / \
 *          2   1     1   2
 *
 *         [1,2,1],   [1,1,2]
 *
 * 输出: false
 */
public class Solution100 {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //p,q不一致
        if(p==null&&q!=null) return false;
        if(p!=null&&q==null) return false;

        //判断左子树、右子树
        if(p!=null&&q!=null){
            if(!isSameTree(p.left,q.left)||!isSameTree(p.right,q.right)) return false;
            if(p.val!=q.val) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        String pstr="12##4##";
        String qstr="12##3##";
        TreeNode p=TreeUtils.stringToTree(pstr);
        TreeNode q=TreeUtils.stringToTree(qstr);
        boolean b = new Solution100().isSameTree(p, q);
        System.out.println(b);
    }
}
```

python

```
```


---

# 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/30-xiang-tong-de-shu.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.
