0
0
DSA Typescriptprogramming~20 mins

Check if Binary Tree is Balanced in DSA Typescript - 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 tree
What is the output of the following TypeScript code that checks if a binary tree is balanced?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function isBalanced(root: TreeNode | null): boolean {
  function height(node: TreeNode | null): number {
    if (!node) return 0;
    const leftHeight = height(node.left);
    if (leftHeight === -1) return -1;
    const rightHeight = height(node.right);
    if (rightHeight === -1) return -1;
    if (Math.abs(leftHeight - rightHeight) > 1) return -1;
    return Math.max(leftHeight, rightHeight) + 1;
  }
  return height(root) !== -1;
}

const tree = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), null);
console.log(isBalanced(tree));
Afalse
Btrue
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees at each node.
Predict Output
intermediate
2:00remaining
Output of balance check on a balanced tree
What is the output of the following TypeScript code that checks if a binary tree is balanced?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function isBalanced(root: TreeNode | null): boolean {
  function height(node: TreeNode | null): number {
    if (!node) return 0;
    const leftHeight = height(node.left);
    if (leftHeight === -1) return -1;
    const rightHeight = height(node.right);
    if (rightHeight === -1) return -1;
    if (Math.abs(leftHeight - rightHeight) > 1) return -1;
    return Math.max(leftHeight, rightHeight) + 1;
  }
  return height(root) !== -1;
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(isBalanced(tree));
Atrue
Bfalse
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Check the height difference between left and right children of the root.
🔧 Debug
advanced
2:00remaining
Identify the error in balance check function
What error does the following TypeScript code produce when checking if a binary tree is balanced?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function isBalanced(root: TreeNode | null): boolean {
  function height(node: TreeNode | null): number {
    if (!node) return 0;
    const leftHeight = height(node.left);
    const rightHeight = height(node.right);
    if (Math.abs(leftHeight - rightHeight) > 1) return -1;
    if (leftHeight === -1 || rightHeight === -1) return -1;
    return Math.max(leftHeight, rightHeight) + 1;
  }
  return height(root) !== -1;
}

const tree = new TreeNode(1);
console.log(isBalanced(tree));
ANo error, outputs true
BNo error, outputs false
CLogical error, returns wrong result
DRuntime error due to incorrect order of checks
Attempts:
2 left
💡 Hint
Check the order of conditions that return -1 in the height function.
🧠 Conceptual
advanced
1:30remaining
Understanding balance condition in binary trees
Which of the following best describes the condition for a binary tree to be balanced?
AEvery node's left and right subtrees have the same number of nodes
BFor every node, the height difference between left and right subtrees is at most 1
CThe tree has the minimum possible height for the number of nodes
DAll leaf nodes are at the same depth
Attempts:
2 left
💡 Hint
Balance depends on height difference, not node count or leaf depth equality.
🚀 Application
expert
2:30remaining
Result of balance check after tree modification
Given the following TypeScript code that builds a binary tree and checks if it is balanced, what is the output after adding a new node to the leftmost leaf?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function isBalanced(root: TreeNode | null): boolean {
  function height(node: TreeNode | null): number {
    if (!node) return 0;
    const leftHeight = height(node.left);
    if (leftHeight === -1) return -1;
    const rightHeight = height(node.right);
    if (rightHeight === -1) return -1;
    if (Math.abs(leftHeight - rightHeight) > 1) return -1;
    return Math.max(leftHeight, rightHeight) + 1;
  }
  return height(root) !== -1;
}

const leaf = new TreeNode(4);
const leftChild = new TreeNode(2, leaf, null);
const rightChild = new TreeNode(3);
const root = new TreeNode(1, leftChild, rightChild);

// Add new node to leftmost leaf
leaf.left = new TreeNode(5);

console.log(isBalanced(root));
ASyntaxError
Btrue
Cfalse
DTypeError
Attempts:
2 left
💡 Hint
Adding a node to the leftmost leaf increases the height of left subtree by 2 levels.