Height Balanced Binary Tree

Tiffany ·2024년 3월 9일
0

AlgoExpert

목록 보기
15/20

        
def getHeight(currNode):
    if currNode is None:
        return -1 
    leftHeight = getHeight(currNode.left)
    rightHeight = getHeight(currNode.right) 
    return max(leftHeight, rightHeight) + 1 

def heightBalancedBinaryTree(tree): 
    #TODO: recursively check the balance of all nodes in the tree 
    #guard closure 
    if tree is None:
        return True
    if abs(getHeight(tree.left) - getHeight(tree.right)) <= 1 and heightBalancedBinaryTree(tree.right) and heightBalancedBinaryTree(tree.left) : 
        return True 
    return False![](https://velog.velcdn.com/images/tiffany_01/post/e5160d97-7179-4a69-b9f2-5e47ddde3631/image.png)
profile
Love what you do and don't quit.

0개의 댓글