-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path190.py
More file actions
26 lines (21 loc) · 718 Bytes
/
190.py
File metadata and controls
26 lines (21 loc) · 718 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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
return self._calcDepth(root) != -1
def _calcDepth(self, root) -> int:
if root is None:
return 0
left_depth = self._calcDepth(root.left)
if left_depth == -1:
return -1
right_depth = self._calcDepth(root.right)
if right_depth == -1:
return -1
if abs(left_depth - right_depth) > 1:
return -1
return 1 + max(left_depth, right_depth)