Update 0110.平衡二叉树.md

This commit is contained in:
QuinnDK
2021-05-13 10:10:53 +08:00
committed by GitHub
parent bff058e3c0
commit 3e60ed1895

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
}
```