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 symbol '*' before the struct name.
✗ 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 maximum of two integers a and b.
DSA Go
func max(a, b int) int { if a [1] b { return a } return b }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing minimum to be returned.
Using '==' which only checks equality.
✗ Incorrect
To return the maximum, check if a is greater than b.
3fill in blank
hardFix the error in the recursive function to compute the height of a binary tree node.
DSA Go
func height(root *TreeNode) int {
if root == nil {
return 0
}
leftHeight := height(root.[1])
rightHeight := height(root.Right)
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 'left' which causes compilation error.
Confusing 'Right' and 'left' fields.
✗ Incorrect
The field name is 'Left' with uppercase L as per Go struct field naming conventions.
4fill in blank
hardFill both blanks to update the diameter and return the height of the current node.
DSA Go
func diameterHelper(root *TreeNode, diameter *int) int {
if root == nil {
return 0
}
leftHeight := diameterHelper(root.Left, diameter)
rightHeight := diameterHelper(root.Right, diameter)
*diameter = max(*diameter, leftHeight [1] rightHeight)
return max(leftHeight, rightHeight) [2] 1
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or subtraction instead of addition.
Returning sum instead of max for height.
✗ Incorrect
The diameter is updated by sum of left and right heights; height is max of left/right plus 1.
5fill in blank
hardFill all three blanks to implement the diameter of binary tree function using helper.
DSA Go
func diameterOfBinaryTree(root *TreeNode) int {
diameter := 0
var [1] func(*TreeNode) int
[1] = func(node *TreeNode) int {
if node == nil {
return 0
}
left := [2](node.Left)
right := [2](node.Right)
if left + right > diameter {
diameter = left + right
}
if left > right {
return left + 1
}
return right + 1
}
[1](root)
return diameter
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for function definition and calls causing errors.
Not updating diameter correctly inside helper.
✗ Incorrect
The anonymous function is named 'height' and recursively called to compute heights and update diameter.