对称的二叉树

深度优先搜索,进行条件判断即可。

 boolean isSymmetrical(TreeNode pRoot) {
        if(pRoot==null) return true;
        return compare(pRoot.left,pRoot.right);
    }

    public boolean compare(TreeNode l,TreeNode r){
        if(l==null&&r==null) return true;
        if(l==null||r==null||l.val!=r.val) return false;
        return compare(l.left,r.right)&&compare(l.right,r.left);
    }


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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