-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check_for_Balanced_Tree.py
51 lines (41 loc) · 1.31 KB
/
Check_for_Balanced_Tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Given a binary tree, find if it is height balanced or not.
A tree is height balanced if difference between heights of left and right subtrees
is not more than one for all nodes of tree.
A height balanced tree An unbalanced tree
1 1
/ \ /
10 39 10
/ /
5 5
Example:
Input: Output: 0 Explanation: The max difference in height of left subtree and right subtree is 2
1
/
2
\
3
Input: Output: 1 Explanation: The max difference in height of left subtree and right subtree is 1.
10
/ \
20 30
/ \
40 60
Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 109
Expected time complexity: O(N)
Expected auxiliary space: O(h) , where h = height of tree
"""
class Solution:
def height(self, root):
if root == None:
return 0
return max(self.height(root.left), self.height(root.right)) + 1
def isBalanced(self, root):
if root == None:
return True
if (abs(self.height(root.left) - self.height(root.right)) <= 1) and self.isBalanced(
root.left) and self.isBalanced(root.right):
return True
return False