算法打卡(四)---------平衡二叉树的判定

力扣:110

什么是平衡二叉树?

二叉树中每个节点的左右子树高度差都不超过1.

平衡二叉树的判断,就是用一个递归依次判定每一个节点,然后利用求节点高度算法来进行比较一下即可。

public boolean isBanance(TreeNode root){
    if(root==null) return true;
    int left=hightNode(root.left);
    int right=hightNode(root.right);
    if(isBanance(root.left)&&isBanance(root.right)&&Math.abs(left-right)<=1) return true;
    else
        return false;

然后计算节点高度算法

public int hightNode(TreeNode root){
if(root==null) return 0;
return Math.max(hightNode(root.right),hightNode(root.left))+1;
}

猜你喜欢

转载自blog.csdn.net/qq_44762290/article/details/113277998