题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function isSymmetrical(pRoot)
{
// write code here
if (pRoot === null) return true
if (pRoot.left === null && pRoot.right === null) {
return true
} else {
const nodeArray = [pRoot.left, pRoot.right]
while (nodeArray.length > 0) {
const head = nodeArray.shift()
const tail = nodeArray.pop()
if (head === null && tail === null) {
continue
} else if (head !== null && tail !== null && head.val === tail.val) {
nodeArray.unshift(head.right)
nodeArray.unshift(head.left)
nodeArray.push(tail.left)
nodeArray.push(tail.right)
} else {
return false
}
}
return true
}
}
module.exports = {
isSymmetrical : isSymmetrical
};
