ð 100. çžåçæ
2022幎10æ10æ¥
- algorithm
ð 100. çžåçæ
éŸåºŠ: ð
é®é¢æè¿°
解æ³
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// æè·¯ïŒ
return mySol(p, q);
}
private boolean mySol(TreeNode a, TreeNode b) {
// éåœç»æ¢æ¡ä»¶
if(a == null) {
return b == null;
}
if(b == null) {
return a == null;
}
// a != null && b != null
if(a.val != b.val) {
return false;
}
return mySol(a.left, b.left) && mySol(a.right, b.right);
}
}