题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return bool布尔型
*/
function compare(a, b) {
if (a === null && b === null) return true;
if (a === null || b === null) return false;
if (a.val!==b.val) return false;
return compare(a.left,b.right)&&compare(a.right,b.left)
}
function isSymmetrical(pRoot) {
if (pRoot === null) return true;
return compare(pRoot.left,pRoot.right)
}
module.exports = {
isSymmetrical: isSymmetrical,
};

叮咚买菜公司氛围 125人发布