/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
height := helper(root)
return height >= 0
}
func helper(node *TreeNode) int {
if node == nil {
return 0
}
left := helper(node.Left)
if left == -1 {
return -1
}
right := helper(node.Right)
if right == -1 {
return -1
}
if abs(left-right) <= 1 {
return max(left, right) + 1
}
return -1
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y int) int {
if x > y {
return y
}
return x
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
height := helper(root)
return height != -1
}
func helper(node *TreeNode) int {
if node == nil {
return 0
}
left := helper(node.Left)
if left == -1 {
return -1
}
right := helper(node.Right)
if right == -1 {
return -1
}
if abs(left-right) <= 1 {
return max(left, right) + 1
}
return -1
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y int) int {
if x > y {
return y
}
return x
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}