package main


/*
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */

/**
 *
 * @param pRoot TreeNode类
 * @return bool布尔型
 */
func IsBalanced_Solution( pRoot *TreeNode ) bool {
    if pRoot == nil {
        return true
    }
    return abs(dfs(pRoot.Left) - dfs(pRoot.Right)) <= 1 && IsBalanced_Solution(pRoot.Left) && IsBalanced_Solution(pRoot.Right)
}

func dfs (node *TreeNode) int {
    if node == nil {
        return 0
    }
    return max(dfs(node.Left), dfs(node.Right)) + 1
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func abs(x int) int {
    if x < 0 {
        return -1 * x
    }
    return x
}