/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcisBalanced(root*TreeNode)bool{ifroot==nil{returntrue}lHeight:=height(root.Left)rHeight:=height(root.Right)returnabs(rHeight-lHeight)<=1&&isBalanced(root.Left)&&isBalanced(root.Right)}funcheight(root*TreeNode)int{ifroot==nil{return0}returnmax(height(root.Left),height(root.Right))+1}funcmax(a,bint)int{ifa>b{returna}returnb}funcabs(numint)int{ifnum<0{return-num}returnnum}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcisBalanced(root*TreeNode)bool{returnheight(root)!=-1}funcheight(root*TreeNode)int{ifroot==nil{return0}lHeight:=height(root.Left)iflHeight==-1{return-1}rHeight:=height(root.Right)ifrHeight==-1||abs(rHeight-lHeight)>1{return-1}returnmax(lHeight,rHeight)+1}funcmax(a,bint)int{ifa>b{returna}returnb}funcabs(numint)int{ifnum<0{return-num}returnnum}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcisBalanced(root*TreeNode)bool{returnheight(root)!=-1}funcheight(root*TreeNode)int{ifroot==nil{return0}lHeight:=height(root.Left)iflHeight==-1{return-1}rHeight:=height(root.Right)ifrHeight==-1||abs(rHeight-lHeight)>1{return-1}returnmax(lHeight,rHeight)+1}funcmax(a,bint)int{ifa>b{returna}returnb}funcabs(numint)int{ifnum<0{return-num}returnnum}