This commit is contained in:
Yiyang Kang 2026-02-08 15:19:01 +09:00
parent 51975f3386
commit 0dbac6b9f0
Signed by: kkyy
SSH key fingerprint: SHA256:lJSbAzC3MvrSORdvIVK6h/3g+rVKJNzM7zq0MgA9WKY
4 changed files with 63 additions and 3 deletions

View file

@ -0,0 +1,36 @@
package q110
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func isBalancedInternal(node *TreeNode, lvl int) (bool, int) {
if node == nil {
return true, lvl
}
lBal, lDpth := isBalancedInternal(node.Left, lvl+1)
rBal, rDpth := isBalancedInternal(node.Right, lvl+1)
dpth := max(lDpth, rDpth)
if !lBal || !rBal || abs(lDpth-rDpth) > 1 {
return false, dpth
}
return true, dpth
}
func isBalanced(root *TreeNode) bool {
ret, _ := isBalancedInternal(root, 0)
return ret
}
var _ = isBalanced