Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Check if Binary Tree is Balanced in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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));
Atrue
Bfalse
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees for each node.
❓ Predict Output
intermediate
2: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));
Atrue
Bfalse
CReferenceError
DRangeError
Attempts:
2 left
💡 Hint
Check the height difference at node 2.
🔧 Debug
advanced
2: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;
}
ASyntaxError
BTypeError
CStackOverflowError
DReturns incorrect result for unbalanced trees
Attempts:
2 left
💡 Hint
Check the return values used to indicate unbalanced condition.
🧠 Conceptual
advanced
2: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?
ATo distinguish unbalanced state from valid subtree heights which are non-negative
BBecause -1 is a valid height for empty trees
CTo cause a runtime error when unbalanced
DBecause JavaScript treats -1 as false in conditions
Attempts:
2 left
💡 Hint
Think about how the function uses return values to signal balance status.
🚀 Application
expert
3: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))
);
A5
B6
C7
D8
Attempts:
2 left
💡 Hint
Check balance for each node's subtree starting from leaves up to root.

Practice

(1/5)
1. What does it mean for a binary tree to be balanced?
easy
A. The tree has the same number of nodes on left and right
B. All nodes have two children
C. The tree is a complete binary tree
D. The height difference between left and right subtrees of every node is at most 1

Solution

  1. Step 1: Understand balance condition

    A balanced binary tree means the height difference between left and right subtrees of every node is at most 1.
  2. Step 2: Compare other options

    Options A, B, and D describe other tree properties but not the balance condition.
  3. Final Answer:

    The height difference between left and right subtrees of every node is at most 1 -> Option D
  4. Quick Check:

    Balance means height difference ≤ 1 [OK]
Hint: Balance means subtree heights differ by no more than 1 [OK]
Common Mistakes:
  • Confusing balanced with complete tree
  • Thinking balance means equal nodes on both sides
  • Assuming all nodes must have two children
2. Which of the following JavaScript function signatures is correct to check if a binary tree is balanced?
easy
A. function checkBalance(tree) { ... }
B. function isBalanced(root) { ... }
C. function balancedCheck(node) { ... }
D. function isBalancedTree() { ... }

Solution

  1. Step 1: Identify common parameter for tree root

    The function should accept the root node as a parameter to check balance.
  2. Step 2: Check function signatures

    Only function isBalanced(root) { ... } uses a clear parameter named 'root' which is standard for tree root nodes.
  3. Final Answer:

    function isBalanced(root) { ... } -> Option B
  4. Quick Check:

    Function needs root parameter [OK]
Hint: Function must accept root node parameter [OK]
Common Mistakes:
  • Omitting root parameter
  • Using unclear or no parameters
  • Using function names that don't indicate balance check
3. What will be the output of the following code snippet?
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function height(node) {
  if (!node) return 0;
  return 1 + Math.max(height(node.left), height(node.right));
}

function isBalanced(root) {
  if (!root) return true;
  let leftHeight = height(root.left);
  let rightHeight = height(root.right);
  if (Math.abs(leftHeight - rightHeight) > 1) return false;
  return isBalanced(root.left) && isBalanced(root.right);
}

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.left.left = new Node(5);

console.log(isBalanced(root));
medium
A. false
B. true
C. undefined
D. Throws error

Solution

  1. Step 1: Calculate heights of left and right subtrees

    Left subtree height is 3 (nodes 2 -> 4 -> 5), right subtree height is 1 (node 3).
  2. Step 2: Check balance condition at root

    Height difference is 3 - 1 = 2, which is greater than 1, so tree is not balanced.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    Height difference > 1 means not balanced [OK]
Hint: Check height difference > 1 returns false [OK]
Common Mistakes:
  • Ignoring deeper subtree height
  • Assuming tree is balanced if root has two children
  • Confusing height with number of nodes
4. Find the bug in this code that checks if a binary tree is balanced:
function isBalanced(root) {
  if (!root) return true;
  let leftHeight = height(root.left);
  let rightHeight = height(root.right);
  if (Math.abs(leftHeight - rightHeight) > 1) return false;
  isBalanced(root.left);
  isBalanced(root.right);
  return true;
}
medium
A. Height function is not defined
B. Incorrect base case for empty tree
C. Missing return statements for recursive calls
D. Using Math.abs incorrectly

Solution

  1. Step 1: Analyze recursive calls

    The calls to isBalanced(root.left) and isBalanced(root.right) are made but their results are not returned or checked.
  2. Step 2: Identify missing return usage

    Without returning these recursive calls, the function always returns true after checking root, ignoring subtree balance.
  3. Final Answer:

    Missing return statements for recursive calls -> Option C
  4. Quick Check:

    Recursive calls must return their results [OK]
Hint: Always return recursive call results [OK]
Common Mistakes:
  • Not returning recursive call results
  • Assuming base case covers all
  • Ignoring subtree balance checks
5. Given a binary tree, which approach is the most efficient to check if it is balanced without recalculating heights multiple times?
hard
A. Use a bottom-up recursion that returns height and balance status together
B. Check balance only at the root node
C. Traverse the tree level by level and count nodes
D. Calculate height for each node separately and check balance

Solution

  1. Step 1: Understand inefficiency of separate height calculations

    Calculating height separately for each node causes repeated work, increasing time complexity.
  2. Step 2: Use bottom-up recursion

    Returning height and balance status together in one recursion avoids repeated height calculations, improving efficiency.
  3. Final Answer:

    Use a bottom-up recursion that returns height and balance status together -> Option A
  4. Quick Check:

    Bottom-up recursion avoids repeated height calls [OK]
Hint: Combine height and balance in one recursion [OK]
Common Mistakes:
  • Recalculating heights multiple times
  • Checking balance only at root
  • Using level order traversal for balance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard