0
0
DSA Javascriptprogramming~10 mins

Height of Binary Tree 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 return the height of a binary tree recursively.

DSA Javascript
function height(node) {
  if (node === null) {
    return 0;
  }
  return 1 + Math.max(height(node.left), height(node[1]));
}
Drag options to blanks, or click blank then click option'
A.parent
B.right
C.left
D.child
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.left twice instead of node.right
Using node.parent which is not part of height calculation
2fill in blank
medium

Complete the code to check if the node is null before calculating height.

DSA Javascript
function height(node) {
  if (node [1] null) {
    return 0;
  }
  return 1 + Math.max(height(node.left), height(node.right));
}
Drag options to blanks, or click blank then click option'
A===
B!==
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== which reverses the logic
Using loose equality == which can cause unexpected results
3fill in blank
hard

Fix the error in the recursive call to height for the right subtree.

DSA Javascript
function height(node) {
  if (node === null) {
    return 0;
  }
  return 1 + Math.max(height(node.left), height(node[1]));
}
Drag options to blanks, or click blank then click option'
A.rigt
B.parent
C.right
D.leftt
Attempts:
3 left
💡 Hint
Common Mistakes
Typos like '.leftt' or '.rigt'
Using '.parent' which is not a child node
4fill in blank
hard

Fill both blanks to create a function that returns the height of a binary tree using recursion.

DSA Javascript
function height(node) {
  if (node [1] null) {
    return 0;
  }
  return 1 + Math.max(height(node[2]), height(node.right));
}
Drag options to blanks, or click blank then click option'
A===
B!==
C.left
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== instead of === in null check
Using '.parent' instead of '.left' for child node
5fill in blank
hard

Fill all three blanks to complete the height function with correct null check and recursive calls.

DSA Javascript
function height(node) {
  if (node [1] null) {
    return 0;
  }
  return 1 + Math.max(height(node[2]), height(node[3]));
}
Drag options to blanks, or click blank then click option'
A!==
B===
C.right
D.left
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== instead of === for null check
Swapping left and right child properties