> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/tree/100.-same-tree.md).

# 100. Same Tree

```java
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        return dfs(p, q);
    }
    
    private boolean dfs(TreeNode first, TreeNode second){
        // preorder
        if(first == null || second == null){
            return first==second;
        }
        if (first.val!=second.val) return false;
        
        if(dfs(first.left, second.left)){
            return dfs(first.right, second.right);
        }else{
            return false;
        }
    }
}
```
