题解 | #对称的二叉树#
对称的二叉树
http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
function isSymmetrical(root)
{
if(!root) return true;
// 递归法
// return dfs(root.left,root.right);
// function dfs(left,right){
// if((!left && right)||(left && !right)) return false;
// if(!left && !right) return true;//这一行与后面一行不许颠倒
// if(left.val!==right.val) return false;
// let dfs_left=dfs(left.left,right.right);
// let dfs_right=dfs(left.right,right.left);
// return dfs_left && dfs_right;
// }
var queue=[root.left,root.right]
while(queue.length){
var leftNode=queue.shift();
var rightNode=queue.shift();
if(!leftNode && !rightNode) continue;
if(!leftNode || !rightNode || leftNode.val !=rightNode.val) return false;
queue.push(leftNode.left);
queue.push(rightNode.right);
queue.push(leftNode.right);
queue.push(rightNode.left);
}
return true;
}