平衡二叉树

private boolean isBalanced=true;

public boolean isBalanced(TreeNode root) {
    if(root==null){
        return true;
    }
    height(root);
    return isBalanced;
}

private int height(TreeNode root){
    if(root==null){
        return 0;
    }
    int left=height(root.left);
    int right=height(root.right);
    if(Math.abs(left-right)>1){
        isBalanced=false;
    }
    return 1+Math.max(left,right);
}

https://www.mianshi.onlinehttps://www.i9code.cn

本文由博客一文多发平台 OpenWrite 发布!

逃跑的眼镜_bvbEK5
7 声望0 粉丝