0
0
DSA Goprogramming~10 mins

Check if Binary Tree is Balanced in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the TreeNode struct with integer value and pointers to left and right children.

DSA Go
type TreeNode struct {
    Val   int
    Left  *[1]
    Right *TreeNode
}
Drag options to blanks, or click blank then click option'
ANode
BTreeNode
CTree
DNodeTree
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different struct name for the child pointers.
Forgetting the pointer * before the struct type.
2fill in blank
medium

Complete the code to calculate the height of a binary tree node recursively.

DSA Go
func height(root *TreeNode) int {
    if root == nil {
        return 0
    }
    leftHeight := height(root.Left)
    rightHeight := height(root.[1])
    if leftHeight > rightHeight {
        return leftHeight + 1
    }
    return rightHeight + 1
}
Drag options to blanks, or click blank then click option'
ARoot
Bleft
Crightt
DRight
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'right' which does not match the struct field.
Typo like 'rightt' or 'left' instead of 'Right'.
3fill in blank
hard

Fix the error in the code that checks if the binary tree is balanced by comparing subtree heights.

DSA Go
func isBalanced(root *TreeNode) bool {
    if root == nil {
        return true
    }
    leftHeight := height(root.Left)
    rightHeight := height(root.Right)
    if abs(leftHeight - rightHeight) > [1] {
        return false
    }
    return isBalanced(root.Left) && isBalanced(root.Right)
}
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which is too strict and rejects balanced trees.
Using negative numbers which are invalid for height difference.
4fill in blank
hard

Fill both blanks to implement the absolute value function used in balance checking.

DSA Go
func abs(x [1]) [2] {
    if x < 0 {
        return -x
    }
    return x
}
Drag options to blanks, or click blank then click option'
Aint
Bfloat64
Cstring
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using float64 which is for decimal numbers.
Using string or bool which are invalid types here.
5fill in blank
hard

Fill all three blanks to optimize balance check by returning height and balance status together.

DSA Go
func checkBalance(root *TreeNode) ([1], [2]) {
    if root == nil {
        return 0, true
    }
    leftHeight, leftBalanced := checkBalance(root.Left)
    rightHeight, rightBalanced := checkBalance(root.[3])
    balanced := leftBalanced && rightBalanced && abs(leftHeight - rightHeight) <= 1
    height := leftHeight + 1
    if rightHeight + 1 > height {
        height = rightHeight + 1
    }
    return height, balanced
}
Drag options to blanks, or click blank then click option'
Aint
Bbool
CRight
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping return types or using wrong child field name.
Using lowercase 'right' instead of 'Right'.