Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a binary tree node with an integer value.
DSA Go
type Node struct {
Value int
Left *Node
Right *[1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-pointer type for child nodes causes incorrect tree structure.
Using unrelated types like int or Tree instead of Node.
✗ Incorrect
The Left and Right fields should be pointers to Node to represent child nodes in the binary tree.
2fill in blank
mediumComplete the code to create a new node with a given integer value.
DSA Go
func NewNode(val int) *Node {
return &Node{Value: [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable like value or Node instead of val.
Trying to access val.Value which does not exist.
✗ Incorrect
The function parameter val holds the integer value to assign to the new node's Value field.
3fill in blank
hardFix the error in the code to assign the left child node correctly.
DSA Go
func (n *Node) SetLeft(child *Node) {
n.Left = [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using &child causes type mismatch (pointer to pointer).
Using *child tries to assign a value instead of a pointer.
✗ Incorrect
The Left field expects a pointer to Node, so assign it directly to child which is already a pointer.
4fill in blank
hardFill both blanks to check if a node is a leaf (no children).
DSA Go
func (n *Node) IsLeaf() bool {
return n.Left == [1] && n.Right == [2]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using true or false instead of nil for pointer checks.
Using 0 which is not valid for pointers.
✗ Incorrect
A leaf node has no children, so both Left and Right pointers should be nil.
5fill in blank
hardFill all three blanks to create a map of node values to their left child values if present.
DSA Go
func LeftChildMap(root *Node) map[int]int {
result := make(map[int]int)
if root.Left != [1] {
result[[2]] = root.Left.[3]
}
return result
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.Left instead of root.Value as map key.
Using root.Left instead of Value to get child's value.
Checking against true or false instead of nil.
✗ Incorrect
Check if Left child is not nil, then map root's Value to Left child's Value field.