题目描述
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
代码片
/*
* @lc app=leetcode.cn id=101 lang=javascript
*
* [101] 对称二叉树
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
//方法一:递归
/**
* 判断一个二叉树是否对称,只需判断二叉树的两个子树是否镜像即可。
* 镜像的条件:
* 1.节点值相等
* 2.node1的左子树和node2的右子树是镜像
* 3.node1的右子树和node2的左子树是镜像
*/
var isSymmetric = function(root) {
if(!root) return true;
//判断两个二叉树是否镜像
const check = (node1, node2) => {
//两个空树,true
if(!node1 && !node2) return true;
//只有一个空树,false
if(!node1 || !node2) return false;
//镜像的条件
return (
node1.val === node2.val &&
check(node1.left, node2.right) &&
check(node1.right, node2.left)
);
};
//判断树的两个子树是否镜像即可
return check(root.left, root.right);
};
//方法二:迭代
//利用层序遍历的思想,一层一层判断,是否对称。
const isSymmetric = root => {
if(!root) return true;
//根节点的左右节点入队
const queue = [root.left, root.right];
while(queue.length) {
//当前队列的长度
const len = queue.length;
//每次对比两个节点,所以i=i+2
for(let i = 0; i < len; i+=2) {
//两个节点出队
const left = queue.shift();
const right = queue.shift();
//有一个为空,另一个不为空,直接返回false
if((left && !right) || (!left && right)) return false;
//两节点都不为空
if(left && right) {
//值不相等,返回false
if(left.val !== right.val) return false;
//将两个节点的左右节点,按照对应关系入队
queue.push(left.left, right.right, left.right, right.left);
}
}
}
//遍历结束返回true
return true;
}
// @lc code=end