forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced-binary-tree.py
More file actions
27 lines (22 loc) · 878 Bytes
/
balanced-binary-tree.py
File metadata and controls
27 lines (22 loc) · 878 Bytes
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
"""
The tree is balanced if
1. Left child and right child are balanced
2. |left_max_depth - right_max_depth| must smaller or equal to 1
So the helper() will check condition 1. and 2. by `if l!=-1 and r!=-1 and abs(l-r)<=1`
if true, the node is balanced, return the depth
if not, the node is not balanced, return -1
"""
class Solution(object):
def isBalanced(self, root):
def helper(node, depth):
if not node.left and not node.right: return depth
l = r = depth #l: left_max_depth, r: right_max_depth
if node.left:
l = helper(node.left, depth+1)
if node.right:
r = helper(node.right, depth+1)
if l!=-1 and r!=-1 and abs(l-r)<=1:
return max(l, r)
return -1
if not root: return True
return helper(root, 0)!=-1