Challenge - 5 Problems
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of balance check on a simple binary tree
What is the output of the following code that checks if a binary tree is balanced?
DSA Javascript
class Node { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function isBalanced(root) { function check(node) { if (!node) return 0; const leftHeight = check(node.left); if (leftHeight === -1) return -1; const rightHeight = check(node.right); if (rightHeight === -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } return check(root) !== -1; } const tree = new Node(1, new Node(2, new Node(4), new Node(5)), new Node(3)); console.log(isBalanced(tree));
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees for each node.
✗ Incorrect
The tree is balanced because for every node, the height difference between left and right subtrees is at most 1.
❓ Predict Output
intermediate2:00remaining
Output of balance check on an unbalanced binary tree
What is the output of the following code that checks if a binary tree is balanced?
DSA Javascript
class Node { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function isBalanced(root) { function check(node) { if (!node) return 0; const leftHeight = check(node.left); if (leftHeight === -1) return -1; const rightHeight = check(node.right); if (rightHeight === -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } return check(root) !== -1; } const tree = new Node(1, new Node(2, new Node(3, new Node(4), null), null), null); console.log(isBalanced(tree));
Attempts:
2 left
💡 Hint
Check the height difference at node 2.
✗ Incorrect
The tree is unbalanced because node 2 has left subtree height 2 and right subtree height 0, difference 2 > 1.
🔧 Debug
advanced2:00remaining
Identify the error in balance check function
What error does the following code produce when checking if a binary tree is balanced?
DSA Javascript
function isBalanced(root) {
function check(node) {
if (!node) return 0;
const leftHeight = check(node.left);
const rightHeight = check(node.right);
if (Math.abs(leftHeight - rightHeight) > 1) return false;
return Math.max(leftHeight, rightHeight) + 1;
}
return check(root) !== false;
}Attempts:
2 left
💡 Hint
Check the return values used to indicate unbalanced condition.
✗ Incorrect
The function returns false inside check but later compares with !== false which can cause incorrect results because check returns numbers or false.
🧠 Conceptual
advanced2:00remaining
Why use -1 as a special value in balance check?
In the balance check function, why do we return -1 to indicate an unbalanced subtree?
Attempts:
2 left
💡 Hint
Think about how the function uses return values to signal balance status.
✗ Incorrect
Returning -1 clearly signals unbalanced subtree because all valid heights are zero or positive, so -1 is a unique marker.
🚀 Application
expert3:00remaining
Determine the number of balanced subtrees in a binary tree
Given the following binary tree, how many subtrees (including single nodes) are balanced?
DSA Javascript
class Node { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } const tree = new Node(1, new Node(2, new Node(4), new Node(5, new Node(7), null) ), new Node(3, null, new Node(6)) );
Attempts:
2 left
💡 Hint
Check balance for each node's subtree starting from leaves up to root.
✗ Incorrect
Counting balanced subtrees: nodes 4,7,5,6,3,2,1 are balanced. Node 5's subtree is balanced because left height 1, right 0 difference 1. Total 7.