```
class Solution {
func isBalanced(_ root: TreeNode?) -> Bool {
guard let root = root else {
return true
}
return heightDFS(root) != -1
}
func heightDFS(_ root: TreeNode?) -> Int {
guard let root = root else {
return 0
}
let leftHeight = heightDFS(root.left)
let rightHeight = heightDFS(root.right)
if leftHeight == -1 || rightHeight == -1 || abs(leftHeight - rightHeight) > 1 {
return -1
}
return max(leftHeight, rightHeight) + 1
}
}
```