题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return bool布尔型
*/
function isSymmetrical( pRoot ) {
// write code here
return boolean(pRoot,pRoot)
}
function boolean(a,b){
//当节点都为空,同时为空,符合
if(a == null &&b == null){
return true
}
//当其中一个为空,不符合
if(a == null || b == null){
return false
}
//当他们的值不相等的时候,不符合
if(a.val != b.val){
return false
}
return boolean(a.left,b.right)&&boolean(a.right,b.left)
}
module.exports = {
isSymmetrical : isSymmetrical
};

查看14道真题和解析
