【剑指offer】55.2 平衡二叉树

75 阅读2分钟

题目描述

在这里插入图片描述

在这里插入图片描述


// 55.2 平衡二叉树

// 力扣
// 输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉
// 树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡
// 二叉树。

// 牛客
// 输入一棵二叉树,判断该二叉树是否是平衡二叉树。在这里,我们只需要
// 考虑其平衡性,不需要考虑其是不是排序二叉树平衡二叉树(Balanced B
// inary Tree),具有以下性质:它是一棵空树或它的左右两个子树的高度
// 差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

题解

///////////////////////////// 遍历检查 /////////////////////////////////

// 力扣
// 不高效的实现,思路可以借鉴
// 构建一个前序遍历,遍历每个结点,在每个结点处调用55题中的深度函数
// 找深度差,每个结点的左右子树的深度差的绝对值不大于1说明是AVL
// 执行用时:90 ms, 在所有 Java 提交中击败了6.49%的用户
// 内存消耗:39 MB, 在所有 Java 提交中击败了5.57%的用户
class Solution {
    public boolean res = true;

    public boolean isBalanced(TreeNode root) {
        if (root == null)
            return res;
        preOrder(root);
        return res;
    }

    private void preOrder(TreeNode root) {
        if (root == null)
            return;
        preOrder(root.left);
        if (Math.abs(searchDepth(root.left) - searchDepth(root.right)) > 1)
            res = false;
        preOrder(root.right);
    }

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


// 牛客
// 运行时间:13ms,超过70.05%用Java提交的代码
// 占用内存:9684KB,超过4.13%用Java提交的代码
public class Solution {
    public boolean res = true;
    
    public boolean IsBalanced_Solution(TreeNode root) {
        if (root == null)
            return res;
        preOrder(root);
        return res;
    }
    
    private void preOrder(TreeNode root) {
        if (root == null)
            return;
        preOrder(root.left);
        if (Math.abs(searchDepth(root.left) - searchDepth(root.right)) > 1)
            res = false;
        preOrder(root.right);
    }

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



///////////////////////////////// 高效实现 //////////////////////



// 力扣
// 高效实现,如果每个结点的左右子树的深度差的绝对值不大于1,则继续递归
// ,如果所有的判断函数isBalanced都是true,最终就会返回true,否则如果if
// 满足不了,就会返回false.
// 执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
// 内存消耗:38.8 MB, 在所有 Java 提交中击败了11.86%的用户
class Solution {

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


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



// 牛客
// 运行时间:11ms,超过83.09%用Java提交的代码
// 占用内存:9680KB,超过4.28%用Java提交的代码
public class Solution {
    
    public boolean IsBalanced_Solution(TreeNode root) {
        if (root == null)
            return true;
        if (Math.abs(searchDepth(root.left) - searchDepth(root.right)) <= 1) {
            return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
        }
        else 
            return false;
    }

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