题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
//传入两颗树root 同时对两棵树进行“先序”遍历(这样既判断了树的结构是否一致,也判断了树的值是否相等)
function preOrder(root1,root2){
if(root1 == null && root2 == null){
return true;
}
if(root1 == null || root2 == null || root1.val != root2.val){
return false;
}
return preOrder(root1.left,root2.right) && preOrder(root1.right,root2.left);
}
function isSymmetrical(pRoot)
{
// write code here
return preOrder(pRoot,pRoot);
}
module.exports = {
isSymmetrical : isSymmetrical
};
#我的实习求职记录#
查看13道真题和解析