Merge pull request #61 from QuinnDK/添加0110平衡二叉树Go版本-1

添加0110平衡二叉树go版本
This commit is contained in:
Carl Sun
2021-05-13 17:26:41 +08:00
committed by GitHub

View File

@ -361,7 +361,40 @@ Python
Go
```Go
func isBalanced(root *TreeNode) bool {
if root==nil{
return true
}
if !isBalanced(root.Left) || !isBalanced(root.Right){
return false
}
LeftH:=maxdepth(root.Left)+1
RightH:=maxdepth(root.Right)+1
if abs(LeftH-RightH)>1{
return false
}
return true
}
func maxdepth(root *TreeNode)int{
if root==nil{
return 0
}
return max(maxdepth(root.Left),maxdepth(root.Right))+1
}
func max(a,b int)int{
if a>b{
return a
}
return b
}
func abs(a int)int{
if a<0{
return -a
}
return a
}
```