Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the height of the tree.
DSA C++
int height(TreeNode* root) {
if (root == nullptr) return [1];
return 1 + std::max(height(root->left), height(root->right));
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for an empty tree.
Returning NULL which is not an integer.
✗ Incorrect
The height of an empty tree is 0.
2fill in blank
mediumComplete the code to check if the height difference between left and right subtrees is more than 1.
DSA C++
bool isBalanced(TreeNode* root) {
if (root == nullptr) return true;
int lh = height(root->left);
int rh = height(root->right);
if (std::abs(lh - rh) > [1]) return false;
return isBalanced(root->left) && isBalanced(root->right);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which means no difference allowed.
Using 2 which is too large.
✗ Incorrect
A binary tree is balanced if the height difference between left and right subtree is at most 1.
3fill in blank
hardFix the error in the height function to avoid redundant calls.
DSA C++
int height(TreeNode* root) {
if (root == nullptr) return 0;
int lh = height(root->left);
int rh = height(root->right);
if (lh == -1 || rh == -1 || std::abs(lh - rh) > 1) return [1];
return 1 + std::max(lh, rh);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 which is a valid height.
Returning INT_MIN which is not standard here.
✗ Incorrect
Return -1 to indicate the tree is not balanced at this node.
4fill in blank
hardFill both blanks to complete the optimized balanced check function.
DSA C++
int checkHeight(TreeNode* root) {
if (root == nullptr) return [1];
int lh = checkHeight(root->left);
if (lh == [2]) return -1;
int rh = checkHeight(root->right);
if (rh == -1) return -1;
if (std::abs(lh - rh) > 1) return -1;
return 1 + std::max(lh, rh);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL which is not an int.
Not checking for -1 to propagate imbalance.
✗ Incorrect
Return 0 for empty tree height and -1 to indicate imbalance.
5fill in blank
hardFill all three blanks to complete the final balanced tree check function.
DSA C++
bool isBalanced(TreeNode* root) {
return checkHeight(root) != [1];
}
int checkHeight(TreeNode* root) {
if (root == nullptr) return [2];
int lh = checkHeight(root->left);
if (lh == [3]) return -1;
int rh = checkHeight(root->right);
if (rh == -1) return -1;
if (std::abs(lh - rh) > 1) return -1;
return 1 + std::max(lh, rh);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL which is not an int.
Not comparing checkHeight result to -1.
✗ Incorrect
The function returns false if checkHeight returns -1 indicating imbalance.
Empty tree height is 0.
-1 signals imbalance.