# 【LeetCode】100. Same Tree 解题报告

## Subject

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

## Solution

### solution 1

    /**
* 0ms
*
* @param p
* @param q
* @return
*/
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null & q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val == q.val) {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
} else {
return false;
}
}

### solution 2

https://leetcode.com/discuss/69708/one-line-java-solution

public boolean isSameTree2(TreeNode p, TreeNode q) {
return (p != null && q != null && p.val == q.val
&& isSameTree2(p.left, q.left) && isSameTree2(p.right, q.right))
|| (p == null && q == null);
}

bingo~~

#### Same Tree -- LeetCode

2014-04-03 04:07:01

#### LeetCode(100)Same Tree

2014-01-09 06:41:21

#### 100. Same Tree [easy] (Python)

2016-05-13 22:13:29

#### [LeetCode]100. Same Tree(相同树)

2017-04-20 00:34:36

#### LeetCode 100 — Same Tree（C++ Java Python）

2014-04-01 20:22:21

#### LeetCode-Same Tree(java)

2014-05-14 09:56:06

#### Leetcode#100. Same Tree(判断两个二叉树相同)

2017-09-27 12:29:08

#### 100. Same Tree 树是否相同

2016-06-27 09:56:20

#### [leetcode] 100. Same Tree 解题报告

2015-12-24 15:40:49

#### LeetCode 100. Same Tree 解题报告

2016-09-29 14:56:43