平衡二叉树

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
概念:
平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
 
解答:
 
 
#求树的高度也可以不用递归,求出整棵树的层数
def height(self, root):
        # write code here
        if not root:
            return 0
        count=0
        s=[root]
        while s:
            temp=[]
            for i in s:
                if i.left:
                    temp.append(i.left)
                if i.right:
                    temp.append(i.right)
            count+=1
            s=temp
        return count
 
 
原文地址:https://www.cnblogs.com/girl1314/p/10475398.html