标签:ini ref == def 队列 写法 boolean || for
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root ==null){
return true;
}
return checkSymmetric(root.left,root.right);
}
public boolean checkSymmetric(TreeNode left,TreeNode right){
if(left == null && right == null){
return true;
}
if(left ==null || right == null){
return false;
}
return left.val == right.val &&
checkSymmetric(left.left,right.right) &&
checkSymmetric(left.right,right.left);
}
}
class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
queue.offer(root);
while(!queue.isEmpty()){
TreeNode left = queue.poll();
TreeNode right = queue.poll();
if(left == null && right == null){
continue;
}
if(left ==null || right == null || left.val != right.val){
return false;
}
queue.offer(left.left);
queue.offer(right.right);
queue.offer(left.right);
queue.offer(right.left);
}
return true;
}
}
标签:ini ref == def 队列 写法 boolean || for
原文地址:https://www.cnblogs.com/CodingXu-jie/p/13936493.html