Challenge - 5 Problems
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Balance Check on Simple Tree
What is the output of the following Go code that checks if a binary tree is balanced?
DSA Go
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)) }
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees for each node.
✗ Incorrect
The tree is balanced because for every node, the height difference between left and right subtrees is at most 1.
The function returns true.
❓ Predict Output
intermediate2:00remaining
Output of Balance Check on Unbalanced Tree
What is the output of the following Go code that checks if a binary tree is balanced?
DSA Go
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)) }
Attempts:
2 left
💡 Hint
Check the height difference at the root node.
✗ Incorrect
The tree is unbalanced because the left subtree is much deeper than the right subtree (which is nil).
The function returns false.
🧠 Conceptual
advanced2:00remaining
Understanding the Balance Check Algorithm
Which statement best describes how the balance check algorithm works in the given code?
Attempts:
2 left
💡 Hint
Think about how the function returns -1 to indicate imbalance.
✗ Incorrect
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.
🔧 Debug
advanced2:00remaining
Identify the Bug in Balance Check Code
What error will the following code produce when run?
DSA Go
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)) }
Attempts:
2 left
💡 Hint
Check if the function stops checking when imbalance is found in subtrees.
✗ Incorrect
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.
🚀 Application
expert3:00remaining
Count Balanced Subtrees in a Binary Tree
Given a binary tree, which code snippet correctly counts how many subtrees are balanced?
DSA Go
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)) }
Attempts:
2 left
💡 Hint
Count every subtree that returns a valid height (not -1).
✗ Incorrect
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.