6.9平衡二叉树(LC110-E)

绝对值函数:abs()

算法:

高度和深度的区别:

节点的高度:节点到叶子节点的距离(从下往上)

节点的深度:节点到根节点的距离(从上往下)

逻辑:一个平衡二叉树的每个节点的左右子树都是平衡二叉树

调试过程:

复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        if self.getheight(root) == 0:
            return True
        else:
            return False

    def getheight(self, node) -> int:
        if node == None:
            return 0        
        leftheight = self.getheight(node.left)
        rightheight = self.getheight(node.right)
        #左子树若有不平衡的,就返回-1
        if leftheight == -1:
            return -1
        #右子树若有不平衡的,就返回-1
        if rightheight == -1:
            return -1
        
        if abs(leftheight-rightheight)>1:
            return -1
        else:
            return 0

原因:问题出在return 0上面,改成return 1 + max(leftheight, rightheight)就好了

**`return 0`**的含义是将节点的高度设置为0,这是不正确的。

正确的做法是使用**`return 1 + max(leftheight, rightheight)`** 来计算节点的高度。这里的**`max(leftheight, rightheight)`**表示选择左子树和右子树中较大的高度作为当前节点的高度,然后再加上1,表示当前节点的高度。

通过这种方式,我们可以确保节点的高度正确地传递到父节点 ,并在比较节点的高度差时得到正确的结果 。如果节点的左子树和右子树高度差超过1,那么在递归过程中会返回-1,最终导致**`isBalanced`**函数返回False。

正确代码:

复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        if self.getheight(root) != -1:
            return True
        else:
            return False

    def getheight(self, node) -> int:
        if node == None:
            return 0        
        leftheight = self.getheight(node.left)
        rightheight = self.getheight(node.right)
        #左子树若有不平衡的,就返回-1
        if leftheight == -1:
            return -1
        #右子树若有不平衡的,就返回-1
        if rightheight == -1:
            return -1
        
        if abs(leftheight-rightheight)>1:
            return -1
        else:
            return 1 + max(leftheight, rightheight)

时间空间复杂度:

时间复杂度:

  • `isBalanced`函数中,我们调用了`getheight`函数来计算每个节点的高度。在最坏情况下,需要遍历二叉树的所有节点,因此时间复杂度为O(n),其中n是二叉树中的节点数。
  • `getheight`函数是一个递归函数,它会遍历二叉树的所有节点。对于每个节点,我们需要递归地计算其左子树和右子树的高度,因此总的时间复杂度也是O(n)。
  • 综上所述,整个算法的时间复杂度为O(n)。

空间复杂度:

  • 在`getheight`函数中,递归调用会产生函数调用栈。在最坏情况下,二叉树是一个完全不平衡的树,即链表形式,此时递归的深度为n,因此空间复杂度为O(n)。
  • 综上所述,整个算法的空间复杂度为O(n)。
相关推荐
qiuiuiu41320 小时前
正点原子RK3568学习日记-GIT
linux·c语言·开发语言·单片机
草莓熊Lotso20 小时前
《C++ STL list 完全指南:从基础操作到特性对比,解锁链表容器高效用法》
开发语言·c++·list
二王一个今21 小时前
Python打包成exe(windows)或者app(mac)
开发语言·python·macos
一勺菠萝丶21 小时前
Mac 上用 Homebrew 安装 JDK 8(适配 zsh 终端)完整教程
java·python·macos
C嘎嘎嵌入式开发1 天前
(2)100天python从入门到拿捏
开发语言·python
Stanford_11061 天前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
Vallelonga1 天前
Rust 中的数组和数组切片引用
开发语言·rust
Kiri霧1 天前
Rust模式匹配详解
开发语言·windows·rust
white-persist1 天前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse1 天前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner