110.平衡二叉树

给定一个二叉树,判断它是否是平衡二叉树

思路:

  1. 获取左右子树的最大高度,求相差,符合则true,不符合则false
  2. 递归,只要有一个不符合,就直接输出false,终止程序
public boolean isBalanced(TreeNode root){
    if(root == null) return true;
    int temp = maxDepth(root.left) - maxDepth(root.right);
    if(temp > 1 || temp < -1) return false;
    if(!isBalanced(root.left) || !isBalanced(root.right)) return false;
    // 如果isBalanced(root.left)代码返回的是false,必定说明这个递归中有temp > 1 || temp < -1,
    // 只要有一个不满足就直接返回false
    return true;
    // 如果递归完毕,都没有返回false,那就说明每个节点的左右子树高度差都不超过1,
    // 是平衡二叉树,返回true

}

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

  

简化:

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

官方解答:

方法一:自顶向下的递归

时间复杂度:O(n * n)

  • 平均情况:n个节点,每个节点遍历logn次
  • 最坏情况:二叉树形成链表,高度为n

空间复杂度:O(n)

  • 取决于递归调用的层数,不超过n层

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

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

方法二:自底向上的递归

时间复杂度:O(n)

  • 从底向上,每个节点只过一遍

空间复杂度:O(n)

  • 取决于height递归的深度,
public boolean isBalanced(TreeNode root){
    return height(root) >= 0; 
}

public int height(TreeNode root){
    if(root == null) return 0;
    // 先递归到二叉树最下面
    int leftHeight = height(root.left);
    int rightHeight = height(root.right);
    if(leftHeight == -1 || rightHeight == -1 || Math.max(leftHeight - rightHeight) > 1){
        return -1;
    } else return Math.max(leftHeight, rightHeight) + 1;
}

相关推荐

  1. 110.平衡

    2024-04-11 12:30:01       37 阅读
  2. [leetcode] 110. 平衡

    2024-04-11 12:30:01       32 阅读
  3. 力扣110:平衡

    2024-04-11 12:30:01       33 阅读
  4. LeetCode110. 平衡

    2024-04-11 12:30:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-11 12:30:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-11 12:30:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-11 12:30:01       82 阅读
  4. Python语言-面向对象

    2024-04-11 12:30:01       91 阅读

热门阅读

  1. Qt事件机制

    2024-04-11 12:30:01       37 阅读
  2. 3.12 Python赋值运算符

    2024-04-11 12:30:01       31 阅读
  3. KISS 原则和 YAGNI原则

    2024-04-11 12:30:01       39 阅读
  4. 「PHP系列」PHP超级全局变量详解

    2024-04-11 12:30:01       28 阅读
  5. 基于Spring Boot的宠物咖啡馆平台的设计与实现

    2024-04-11 12:30:01       37 阅读
  6. 【Linux】tcpdump P1 - 网络过滤选项

    2024-04-11 12:30:01       42 阅读
  7. git修改某个远端服务器的地址的方式以及4种remote

    2024-04-11 12:30:01       35 阅读
  8. 【报错】Not allowed to load local resource:...

    2024-04-11 12:30:01       29 阅读
  9. STL--容器

    2024-04-11 12:30:01       32 阅读
  10. vue动态绑定class的几种方法

    2024-04-11 12:30:01       38 阅读
  11. 养牛场污水处理的方法和运用的工艺

    2024-04-11 12:30:01       36 阅读
  12. SpringBoot-如何设计优秀的后端接口?

    2024-04-11 12:30:01       33 阅读