0
0
DSA Javascriptprogramming~10 mins

Check if Binary Tree is Balanced in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a node is null.

DSA Javascript
if (node [1] null) return true;
Drag options to blanks, or click blank then click option'
A!==
B===
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '===' which can cause unexpected type coercion.
Using '!=' or '!==' incorrectly.
2fill in blank
medium

Complete the code to calculate the height of a node recursively.

DSA Javascript
function height(node) {
  if (node === null) return 0;
  return 1 + Math.max(height(node.left), [1]);
}
Drag options to blanks, or click blank then click option'
Aheight(node.right)
Bheight(node.parent)
Cheight(node)
Dheight(node.left)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling height on node.left twice instead of node.right.
Calling height on node or node.parent which causes infinite recursion or wrong result.
3fill in blank
hard

Fix the error in the balance check condition.

DSA Javascript
if (Math.abs(height(node.left) - height(node.right)) [1] 1) return false;
Drag options to blanks, or click blank then click option'
A>
B<=
C===
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' which incorrectly returns false for balanced nodes.
Using '===' which only checks exact difference of 1.
4fill in blank
hard

Fill both blanks to complete the recursive balanced tree check.

DSA Javascript
return isBalanced(node.[1]) && isBalanced(node.[2]);
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent' or 'child' which are not valid properties for this check.
5fill in blank
hard

Fill all three blanks to complete the full isBalanced function.

DSA Javascript
function isBalanced(node) {
  if (node [1] null) return true;
  if (Math.abs(height(node.left) - height(node.right)) [2] 1) return false;
  return isBalanced(node.[3]) && isBalanced(node.right);
}
Drag options to blanks, or click blank then click option'
A===
B>
Cleft
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' in height difference check.
Using 'parent' or other invalid properties.