100.Same Tree

Solution 1: accepted 0ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) { // both p, q are null
return true;
}
else if (p != null && q != null) { // both p, q are not null
return (p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right));
}
else { // one of p, q is null
return false;
}
}
}