blob: ba741d2a7d462dcc2596ca3c7dfad76f76a6cb67 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def get_height(root):
if root is None:
return 0
return max(get_height(root.left), get_height(root.right)) + 1
def is_tree_balanced(root):
if root is None:
return True
lh = get_height(root.left)
rh = get_height(root.right)
if (abs(lh - rh) <= 1) and is_tree_balanced(
root.left) is True and is_tree_balanced( root.right) is True:
return True
return False
|