0
0
DSA Typescriptprogramming~10 mins

Tree Terminology Root Leaf Height Depth Level 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 create the root node of a tree.

DSA Typescript
const root = new TreeNode([1]);
Drag options to blanks, or click blank then click option'
Anull
B0
Cundefined
DrootValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or undefined as the root node value.
2fill in blank
medium

Complete the code to check if a node is a leaf (has no children).

DSA Typescript
function isLeaf(node: TreeNode): boolean {
  return node.children.length [1] 0;
}
Drag options to blanks, or click blank then click option'
A>
B===
C<
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < instead of === to check for zero children.
3fill in blank
hard

Fix the error in the function that calculates the depth of a node.

DSA Typescript
function getDepth(node: TreeNode): number {
  if (!node.parent) return [1];
  return 1 + getDepth(node.parent);
}
Drag options to blanks, or click blank then click option'
A0
Bnull
C-1
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null or undefined instead of 0 for root depth.
4fill in blank
hard

Complete the code to calculate the height of a node.

DSA Typescript
function getHeight(node: TreeNode): number {
  if (node.children.length === 0) return [1];
  return 1 + Math.max(...node.children.map(child => getHeight(child)));
}
Drag options to blanks, or click blank then click option'
A0
B1
C)
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 for leaf height or missing closing parenthesis.
5fill in blank
hard

Fill all three blanks to create a function that returns the level of a node in the tree.

DSA Typescript
function getLevel(node: TreeNode): number {
  let level = 0;
  let current = node;
  while (current.[1]) {
    level [2] level + 1;
    current = current.[3];
  }
  return level;
}
Drag options to blanks, or click blank then click option'
Aparent
B=
C+=
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment instead of increment, or wrong property name.