mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0110.平衡二叉树.md
This commit is contained in:
@ -361,7 +361,40 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user