Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the height of a binary tree node.
DSA Javascript
function height(node) {
if (node === null) return 0;
return 1 + Math.max(height(node.left), height(node.[1]));
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' instead of 'right' causes undefined errors.
Using 'child' or 'root' are not valid properties of a node.
✗ Incorrect
The height function calculates the height of a node by recursively checking the left and right children. The correct property to access the right child is 'right'.
2fill in blank
mediumComplete the code to update the diameter during traversal.
DSA Javascript
function diameterOfBinaryTree(root) {
let diameter = 0;
function dfs(node) {
if (node === null) return 0;
let leftHeight = dfs(node.left);
let rightHeight = dfs(node.right);
diameter = Math.max(diameter, leftHeight + rightHeight);
return 1 + Math.max(leftHeight, [1]);
}
dfs(root);
return diameter;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning diameter instead of rightHeight causes wrong height calculation.
Returning node or root is incorrect as they are objects, not heights.
✗ Incorrect
The function returns the height of the current node, which is 1 plus the maximum height of its left and right children. So, it should return 1 + Math.max(leftHeight, rightHeight).
3fill in blank
hardFix the error in the recursive function call to avoid infinite recursion.
DSA Javascript
function dfs(node) {
if (node === null) return 0;
let leftHeight = dfs(node.left);
let rightHeight = dfs(node.[1]);
return 1 + Math.max(leftHeight, rightHeight);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' twice causes missing right subtree traversal.
Using 'parent' causes infinite recursion or errors.
✗ Incorrect
The recursive call should be on the right child of the node, which is accessed by 'right'. Using 'parent' or 'node' would cause errors or infinite recursion.
4fill in blank
hardFill both blanks to correctly update the diameter and return the height.
DSA Javascript
function dfs(node) {
if (node === null) return 0;
let leftHeight = dfs(node.left);
let rightHeight = dfs(node.right);
diameter = Math.max(diameter, leftHeight [1] rightHeight);
return 1 [2] Math.max(leftHeight, rightHeight);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '*' causes wrong diameter or height calculations.
Using '/' causes runtime errors or wrong results.
✗ Incorrect
The diameter is updated by adding leftHeight and rightHeight. The height is 1 plus the maximum of leftHeight and rightHeight, so both blanks use '+'.
5fill in blank
hardFill all three blanks to complete the diameter calculation using a helper function.
DSA Javascript
function diameterOfBinaryTree(root) {
let diameter = 0;
function dfs(node) {
if (node === null) return 0;
let leftHeight = dfs(node.left);
let rightHeight = dfs(node.right);
diameter = Math.max(diameter, leftHeight [1] rightHeight);
return 1 + Math.max(leftHeight, [2]);
}
dfs([3]);
return diameter;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators like '-' or '*' for diameter update.
Returning wrong variable instead of rightHeight.
Calling dfs on leftHeight or rightHeight instead of root.
✗ Incorrect
The diameter is updated by adding leftHeight and rightHeight (use '+'). The height returned is 1 plus the max of leftHeight and rightHeight. The dfs function is called on the root node.