0
0
DSA Typescriptprogramming~10 mins

Height of Binary Tree in DSA Typescript - 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.

DSA Typescript
function height(root: TreeNode | null): number {
  if (root === null) {
    return [1];
  }
  return 1 + Math.max(height(root.left), height(root.right));
}
Drag options to blanks, or click blank then click option'
Anull
B1
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 for a null node.
Returning null which causes errors.
2fill in blank
medium

Complete the code to calculate the height by comparing left and right subtree heights.

DSA Typescript
return 1 + Math.[1](height(root.left), height(root.right));
Drag options to blanks, or click blank then click option'
Amax
Bmin
Csum
Dabs
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max which gives wrong height.
Using sum which overcounts height.
3fill in blank
hard

Fix the error in the base case to correctly handle empty nodes.

DSA Typescript
if (root [1] null) {
  return 0;
}
Drag options to blanks, or click blank then click option'
A==
B!==
C!=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which can cause unexpected behavior.
Using != or !== which check for inequality.
4fill in blank
hard

Fill both blanks to complete the recursive height calculation.

DSA Typescript
const leftHeight = height(root.[1]);
const rightHeight = height(root.[2]);
return 1 + Math.max(leftHeight, rightHeight);
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.
Mixing left and right properties.
5fill in blank
hard

Fill all three blanks to create a function that returns the height of a binary tree.

DSA Typescript
function height([1]: TreeNode | null): number {
  if ([2] === null) {
    return [3];
  }
  return 1 + Math.max(height([2].left), height([2].right));
}
Drag options to blanks, or click blank then click option'
Aroot
C0
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Returning 1 instead of 0 for null nodes.