平衡二叉树

平衡二叉树

http://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222

根据平衡二叉树的定义,一颗平衡二叉树的左右子树都是平衡二叉树且他们的高度差不超过1
注:空树是平衡二叉树

 public boolean IsBalanced_Solution(TreeNode root) {

        if(root==null) return true;

        return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right)&&
                (Math.abs(TreeDepth(root.left)-TreeDepth(root.right))<=1);
    }

    private int TreeDepth(TreeNode root) {

        if(root==null)return 0;

        return Math.max(TreeDepth(root.left)+1,TreeDepth(root.right)+1);

    }
全部评论

相关推荐

点赞 评论 收藏
分享
xwqlikepsl:感觉很厉害啊,慢慢找
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务