0
0
DSA Javascriptprogramming~20 mins

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

Choose your learning style9 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.