[Ruby] O(n)
\n# Definition for a binary tree node.\n# class TreeNode\n#     attr_accessor :val, :left, :right\n#     def initialize(val = 0, left = nil, right = nil)\n#    
```\n# Definition for a binary tree node.\n# class TreeNode\n#     attr_accessor :val, :left, :right\n#     def initialize(val = 0, left = nil, right = nil)\n#         @val = val\n#         @left = left\n#         @right = right\n#     end\n# end\n# @param {TreeNode} root\n# @return {Boolean}\ndef is_balanced(root)\n  balanced_height(root, 0) >= 0\nend\n\ndef balanced_height(root, height)\n  return height unless root\n\n  l = balanced_height(root.left, height + 1)\n  r = balanced_height(root.right, height + 1)\n\n  return -1 if l == -1 || r == -1 || (l - r).abs > 1\n\n  [l, r].max\nend\n```
['Ruby']