Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different struct name for the child pointers.
Forgetting the pointer * before the struct type.
✗ Incorrect
The left child pointer must be of type *TreeNode to correctly reference the same struct type.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'right' which does not match the struct field.
Typo like 'rightt' or 'left' instead of 'Right'.
✗ Incorrect
The right child of the node is accessed by root.Right with capital R matching the struct field.
3fill in blank
hardFix 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'
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.
✗ Incorrect
A binary tree is balanced if the height difference between left and right subtrees is at most 1.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using float64 which is for decimal numbers.
Using string or bool which are invalid types here.
✗ Incorrect
The abs function takes and returns an int because tree heights are integers.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping return types or using wrong child field name.
Using lowercase 'right' instead of 'Right'.
✗ Incorrect
The function returns height as int and balance status as bool; the right child is accessed by root.Right.