Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the TreeNode class with left and right children.
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = [1]; this.right = null; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null for children.
Assigning 0 or this which are incorrect types.
✗ Incorrect
The left child should be initialized to null to indicate no child initially.
2fill in blank
mediumComplete the code to calculate the maximum of two numbers.
DSA Typescript
function max(a: number, b: number): number { return a [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which adds numbers instead of comparing.
Using '>' alone which returns a boolean, not a number.
Using Math.max(a, b) inside return without proper syntax.
✗ Incorrect
The ternary operator 'a > b ? a : b' returns the maximum of a and b.
3fill in blank
hardFix the error in the recursive function to compute the height of a binary tree node.
DSA Typescript
function height(node: TreeNode | null): number {
if (node === null) return 0;
return 1 + Math.max(height(node.left), [1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling height(node.left) twice instead of right child.
Using node.right directly instead of recursive call.
✗ Incorrect
To compute height, we must compare left and right subtree heights recursively.
4fill in blank
hardFill both blanks to update the diameter while computing height.
DSA Typescript
function diameterOfBinaryTree(root: TreeNode | null): number {
let diameter = 0;
function height(node: TreeNode | null): number {
if (node === null) return 0;
const leftHeight = height(node.left);
const rightHeight = height(node.right);
diameter = Math.max(diameter, [1] + [2]);
return 1 + Math.max(leftHeight, rightHeight);
}
height(root);
return diameter;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using diameter variable inside Math.max incorrectly.
Adding 1 to heights when updating diameter.
✗ Incorrect
The diameter is updated by the sum of left and right subtree heights at each node.
5fill in blank
hardFill all three blanks to complete the diameter calculation function with proper types and return.
DSA Typescript
function diameterOfBinaryTree(root: [1]): [2] { let diameter = 0; function height(node: [3]): number { if (node === null) return 0; const leftHeight = height(node.left); const rightHeight = height(node.right); diameter = Math.max(diameter, leftHeight + rightHeight); return 1 + Math.max(leftHeight, rightHeight); } height(root); return diameter; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type instead of number.
Using TreeNode without null for parameters.
✗ Incorrect
The root parameter and node parameter are TreeNode or null; the function returns a number representing diameter.