Python世界:力扣题110,平衡二叉树判别,easy

Python世界:力扣题110,平衡二叉树判别,easy

任务背景


问题来自力扣题目:110 Balanced Binary Tree,大意如下:

Given a binary tree, determine if it is height-balanced

翻译下,需求是:判断给定二叉数是否高度平衡。

思路分析


想练手下二叉树的遍历,结果在easy级上踩了坑,容我细细道来。注意本题中前置条件已默认是二叉树输入,不用考虑非二叉的输入场景。

于是,我把题意理解为,求该树中最小遍历深度和最大遍历深度,两者之差不应超过1.

python 复制代码
# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

depth = 0
depth_min = 2**31
depth_max = -1
class Solution(object):
    def isBalanced(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: bool
        """

        flag_res = True
        if root == None:
            return flag_res

        def recursive(node):
            global depth, depth_max, depth_min
            if node == None:
                depth_max = max(depth_max, depth)
                depth_min = min(depth_min, depth)
                return
            print(node.val)
            depth = depth + 1
            recursive(node.left)
            recursive(node.right)
            depth = depth - 1

        # 重要:每次需重新初始化全局变量,否则深度最值不准
        global depth, depth_max, depth_min
        depth_min = 2**31
        depth_max = -1
        recursive(root)
        if (depth_max - depth_min > 1):
            print(depth_max, depth_min)
            flag_res = False

        return flag_res

初步用例通过后,提交发现这个用例错误:

python 复制代码
# case true 错误理解,失败用例
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.left.left.left = TreeNode(8)
root.right.right = TreeNode(3)
root.right.left = TreeNode(6)

才幡然醒悟,题意理解偏了,二叉树是否平衡,本质问的是:平衡树指每个节点的左右两个子树深度差异最大不超过2。

所以,我们应该求:对每个左右子树求取最大深度,比较左右子树差异。

代码实现


python 复制代码
# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution(object):
    def isBalanced(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: bool
        """
        # get the max depth of tree
        def recursive(node):
            if node == None:
                return 0
            depth_left = recursive(node.left)
            depth_right = recursive(node.right)
            if depth_left < 0 or depth_right < 0:
                return -1 # 已有子树不平衡
            if abs(depth_left - depth_right) > 1:
                return -1 # 当前不平衡
            return max(depth_left, depth_right) + 1 # 左右子树深度上提

        depth = recursive(root)
        return (depth >= 0) # 非负则平衡,负则不平衡

测试套件


单例测试版主调:

python 复制代码
# case true
root = TreeNode(1)

# case true
root = None

# case true
root = TreeNode(0)
root.left = TreeNode(1)
root.right = TreeNode(2)

# case false
root = TreeNode(1)
root.right = TreeNode(2)
root.right.left = TreeNode(3)

# case true
root = TreeNode(0)
root.left = TreeNode(1)
root.right = TreeNode(2)
root.right.right = TreeNode(3)

# case true 错误理解,失败用例
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.left.left.left = TreeNode(8)
root.right.right = TreeNode(3)
root.right.left = TreeNode(6)


sol = Solution()
res = sol.isBalanced(root)
print(res)

测试套版主调:

python 复制代码
import unittest


def test_base(self, root, ret):
    sol = Solution()
    res = sol.isBalanced(root)
    self.assertEqual(res, ret)


# 编写测试套
class TestSol(unittest.TestCase):
    def test_special1(self):
        ret = True
        root = TreeNode(1)
        test_base(self, root, ret)

    def test_special2(self):
        ret = True
        root = None
        test_base(self, root, ret)

    def test_common1(self):
        ret = True
        root = TreeNode(0)
        root.left = TreeNode(1)
        root.right = TreeNode(2)
        test_base(self, root, ret)

    def test_common2(self):
        ret = False
        root = TreeNode(1)
        root.right = TreeNode(2)
        root.right.left = TreeNode(3)
        test_base(self, root, ret)

    def test_common3(self):
        ret = True
        root = TreeNode(0)
        root.left = TreeNode(1)
        root.right = TreeNode(2)
        root.right.right = TreeNode(3)
        test_base(self, root, ret)

    def test_common4(self):
        ret = True # 错误理解,失败用例
        root = TreeNode(1)
        root.left = TreeNode(2)
        root.right = TreeNode(3)
        root.left.left = TreeNode(4)
        root.left.right = TreeNode(5)
        root.left.left.left = TreeNode(8)
        root.right.right = TreeNode(3)
        root.right.left = TreeNode(6)


# 测试套版本主调
if __name__ == '__main__':
    print('start!')
    unittest.main() # 启动单元测试
    print('done!')

本文小结


呜乎,通过本文实践,简单的事情切不可大意,再次感受到:做正确的事,比正确的做事更重要!

相关参考:https://leetcode.com/problems/balanced-binary-tree/solutions/2428871/very-easy-100-fully-explained-c-java-python-javascript-python3/

相关推荐
艾莉丝努力练剑3 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
橡晟6 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子6 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜37 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian7 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
企鹅与蟒蛇7 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba7 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
珊瑚里的鱼8 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上8 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang8 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab