请实现一个函数,用来判断一颗二叉树是不是对称的。

function isSymmetrical(pRoot){
    return testCorrect( pRoot , pRoot );
}

function testCorrect(root1 , root2)
{
    // write code here
    if( root1 == null && root2 == null ){
        return true;
    }
    if( root1 == null || root2 == null ){
        return false;
    }
    if( root1.val != root2.val ){
        return false;
    }
    return testCorrect( root1.right , root2.left ) && testCorrect( root1.left , root2.right );
    
    /*
    if( pRoot == null ){
        return false;
    }
    if( pRoot.left != null && pRoot.right == null ){
       return true; 
    }
    if( pRoot.right != null && pRoot.left == null ){
       return true; 
    }
    while( pRoot.left != null && pRoot.right != null ){
        if( pRoot.left.val == pRoot.right.val ){
            return true;
        }
    }
    */
}

全部评论

相关推荐

12-15 11:27
门头沟学院 Java
哇哇的菜鸡oc:所有人不要理会,就好了,后面他就知道怎么回事了,只能说有的时候市场都是被宰的人搞坏的
点赞 评论 收藏
分享
12-15 12:50
河北工程大学
sta666:我也是这个国际商业化的,三天,一天一面,就通过了,不过我是后端实习生,好好面感觉能过。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务