三刷
98.31 %
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
boolean same = false;
if (p == null && q == null) {
same = true;
} else if (p != null && q != null && p.val == q.val) {
same = isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
return same;
}
}
二刷
21.91 %
public class Solution {
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);
}
return false;
}
}
一刷
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
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 false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
No comments:
Post a Comment