//给你一个二叉树的根节点 root , 检查它是否轴对称。
//
//
//
// 示例 1:
//
//
//输入:root = [1,2,2,3,4,4,3]
//输出:true
//
//
// 示例 2:
//
//
//输入:root = [1,2,2,null,3,null,3]
//输出:false
//
//
//
//
// 提示:
//
//
// 树中节点数目在范围 [1, 1000] 内
// -100 <= Node.val <= 100
//
//
//
//
// 进阶:你可以运用递归和迭代两种方法解决这个问题吗?
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 2600 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
/**
* 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 isSymmetric(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.offerFirst(root.left);
deque.offerFirst(root.right);
/*
仅一边为null则认为不匹配;两边非null但不匹配,则认为不匹配;
两边都匹配,则继续下一次比较
*/
while (!deque.isEmpty()){
TreeNode curLeft = deque.pollFirst();
TreeNode curRight = deque.pollLast();
if(curLeft == null && curRight == null){
continue;
}
if(curLeft == null && curRight != null){
return false;
}
if(curLeft != null && curRight == null){
return false;
}
if(curLeft.val != curRight.val){
return false;
}
deque.offerFirst(curLeft.right);
deque.offerFirst(curLeft.left);
deque.offerLast(curRight.left);
deque.offerLast(curRight.right);
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)