zoukankan      html  css  js  c++  java
  • 牛客网每日一练

    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    #
    # 
    # @param root TreeNode类 
    # @return bool布尔型
    #
    class Solution:
        def isBalanced(self , root ):
            # write code here
            def calculate(root):
                if not root:
                    return True,0
                elif not root.left and not root.right:
                    return True,1
                else:
                    if root.left:
                        Lbalance, Lheight = calculate(root.left)
                    else:
                        Lbalance, Lheight = True,0
                    if root.right:
                        Rbalance, Rheight = calculate(root.right)
                    else:
                        Rbalance, Rheight = True, 0
                    if Lbalance and Rbalance and abs(Lheight - Rheight)<=1:
                        return True, max(Lheight, Rheight) + 1
                    else:
                        return False , -1
            balance, height = calculate(root)
            return(balance)

    此题为牛客网中经典必刷题,题目为
    本题要求判断给定的二叉树是否是平衡二叉树
    平衡二叉树的性质为: 要么是一棵空树,要么任何一个节点的左右子树高度差的绝对值不超过 1。
    一颗树的高度指的是树的根节点到所有节点的距离中的最大值。
     
    本题使用递归的思想,将判断平衡与高度分开计算
    我在此题中掌握了分开计算的思想
  • 相关阅读:
    数据类型补充
    Kubernetes1.18.5 Cilium安装
    Cilium安装要求
    CentOS内核升级
    临时存储 Ephemeral Storage
    Pod优先级
    kubelet 垃圾回收策略
    Kubernetes Eviction驱逐
    根据PID查看具体的容器
    Kubernetes 资源预留(二)
  • 原文地址:https://www.cnblogs.com/nenu/p/14571196.html
Copyright © 2011-2022 走看看