Rename bst_is_balanced.py to bt_is_balanced.py

This commit is contained in:
bt3gl 2023-08-08 15:04:04 -07:00 committed by GitHub
parent 39922339a3
commit 75bb0277cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

21
trees/bt_is_balanced.py Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def height(root):
if root is None:
return -1
return 1 + max(height(root.left), height(root.right))
def is_balanced(root):
if root is None:
return True
return abs(height(root.left) - height(root.right)) < 2 and \
is_balanced(root.left) and is_balanced(root.right)