0
0
DSA Goprogramming~20 mins

Check if Binary Tree is Balanced in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
}
Afalse
Btrue
Cruntime error
Dcompilation error
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees for each node.
Predict Output
intermediate
2: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))
}
Acompilation error
Btrue
Cruntime error
Dfalse
Attempts:
2 left
💡 Hint
Check the height difference at the root node.
🧠 Conceptual
advanced
2:00remaining
Understanding the Balance Check Algorithm
Which statement best describes how the balance check algorithm works in the given code?
AIt only checks leaf nodes to determine if the tree is balanced.
BIt traverses the tree in level order and counts nodes to decide balance.
CIt calculates the height of left and right subtrees and returns false immediately if difference is more than 1.
DIt uses a stack to store nodes and checks balance after traversal.
Attempts:
2 left
💡 Hint
Think about how the function returns -1 to indicate imbalance.
🔧 Debug
advanced
2: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))
}
AIncorrect balance result due to missing imbalance check propagation
BNo error, outputs false
CStack overflow due to infinite recursion
DNo error, outputs true
Attempts:
2 left
💡 Hint
Check if the function stops checking when imbalance is found in subtrees.
🚀 Application
expert
3: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))
}
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Count every subtree that returns a valid height (not -1).