Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return 0 when the node is null.
DSA C#
if (node == null) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for null node.
Returning null which causes errors.
✗ Incorrect
When the node is null, the height is 0 because there are no nodes below.
2fill in blank
mediumComplete the code to calculate the height of the left subtree.
DSA C#
int leftHeight = [1](node.left);Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names causing compile errors.
Using uppercase 'Height' if method is lowercase.
✗ Incorrect
The method name is 'height' and it is called recursively on the left child.
3fill in blank
hardFix the error in returning the height by adding 1 to the maximum of left and right heights.
DSA C#
return 1 + Math.[1](leftHeight, rightHeight);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.Min which returns the smaller value.
Using incorrect method names like Maximum.
✗ Incorrect
We use Math.Max to get the larger height between left and right subtrees.
4fill in blank
hardFill both blanks to calculate the height of left and right subtrees recursively.
DSA C#
int leftHeight = [1](node.left); int rightHeight = [2](node.right);
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names causing errors.
Using uppercase when method is lowercase.
✗ Incorrect
The method 'height' is called recursively on both left and right children.
5fill in blank
hardFill all three blanks to complete the height function of a binary tree.
DSA C#
public int height(Node node) {
if (node == null) return [1];
int leftHeight = [2](node.left);
int rightHeight = [3](node.right);
return 1 + Math.Max(leftHeight, rightHeight);
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 for null node.
Using different method names in recursive calls.
✗ Incorrect
Return 0 for null node, then recursively call 'height' on left and right children.
