package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func isBalanced(root *TreeNode) bool { var check func(node *TreeNode) int check = func(node *TreeNode) int { if node == nil { return 0 } left := check(node.Left) if left == -1 { return -1 } right := check(node.Right) if right == -1 { return -1 } if abs(left-right) > 1 { return -1 } if left > right { return left + 1 } return right + 1 } return check(root) != -1 } func abs(x int) int { if x < 0 { return -x } return x } func main() { root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Right = &TreeNode{Val: 3} root.Left.Left = &TreeNode{Val: 4} root.Left.Right = &TreeNode{Val: 5} fmt.Println(isBalanced(root)) }
The tree is balanced because for every node, the height difference between left and right subtrees is at most 1.
The function returns true.
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func isBalanced(root *TreeNode) bool { var check func(node *TreeNode) int check = func(node *TreeNode) int { if node == nil { return 0 } left := check(node.Left) if left == -1 { return -1 } right := check(node.Right) if right == -1 { return -1 } if abs(left-right) > 1 { return -1 } if left > right { return left + 1 } return right + 1 } return check(root) != -1 } func abs(x int) int { if x < 0 { return -x } return x } func main() { root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Left.Left = &TreeNode{Val: 3} root.Left.Left.Left = &TreeNode{Val: 4} fmt.Println(isBalanced(root)) }
The tree is unbalanced because the left subtree is much deeper than the right subtree (which is nil).
The function returns false.
The algorithm uses recursion to get heights of left and right subtrees.
If the difference is more than 1, it returns -1 to indicate imbalance immediately.
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func isBalanced(root *TreeNode) bool { var check func(node *TreeNode) int check = func(node *TreeNode) int { if node == nil { return 0 } left := check(node.Left) right := check(node.Right) if abs(left-right) > 1 { return -1 } if left > right { return left + 1 } return right + 1 } return check(root) != -1 } func abs(x int) int { if x < 0 { return -x } return x } func main() { root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Left.Left = &TreeNode{Val: 3} root.Left.Left.Left = &TreeNode{Val: 4} fmt.Println(isBalanced(root)) }
The code does not check if left or right subtree returned -1 (imbalance) before continuing.
This causes incorrect results because imbalance is not propagated up.
package main import "fmt" type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func countBalancedSubtrees(root *TreeNode) int { count := 0 var check func(node *TreeNode) int check = func(node *TreeNode) int { if node == nil { return 0 } left := check(node.Left) if left == -1 { return -1 } right := check(node.Right) if right == -1 { return -1 } if abs(left-right) > 1 { return -1 } count++ if left > right { return left + 1 } return right + 1 } check(root) return count } func abs(x int) int { if x < 0 { return -x } return x } func main() { root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Right = &TreeNode{Val: 3} root.Left.Left = &TreeNode{Val: 4} root.Left.Right = &TreeNode{Val: 5} fmt.Println(countBalancedSubtrees(root)) }
The balanced subtrees are the leaf nodes (4,5,3), the subtree rooted at 2, and the whole tree rooted at 1.
Total balanced subtrees = 5.
