package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
type TreeNode = structures.TreeNode
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
leftHight := depth(root.Left)
rightHight := depth(root.Right)
return abs(leftHight-rightHight) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)
}
func depth(root *TreeNode) int {
if root == nil {
return 0
}
return max(depth(root.Left), depth(root.Right)) + 1
}
func abs(a int) int {
if a > 0 {
return a
}
return -a
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}