比较简单的递归思路

对称的二叉树

http://www.nowcoder.com/questionTerminal/ff05d44dfdb04e1d83bdbdab320efbcb

boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot==null) return true;
        TreeNode pLeft = pRoot.left;
        TreeNode pRight = pRoot.right;
        boolean res = help(pLeft,pRight);
        return res;
    }
    public boolean help(TreeNode pLeft,TreeNode pRight){
        if(pLeft==null && pRight==null )return true;
        //下面是两种不对称的情况:一是左右节点一个为空一个不为空,二是左右节点值不想等
        if(pLeft!=null&&pRight==null) return false;
        if(pLeft==null&&pRight!=null) return false;
        if(pLeft.val != pRight.val){
             return false;
        }else{//递归去判断下面的情况
            return help(pLeft.left,pRight.right)&& help(pLeft.right,pRight.left);
        }

    }
全部评论

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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