Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Tree Terminology Root Leaf Height Depth Level in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
šŸŽ–ļø
Tree Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Output of Tree Node Levels
What is the printed level of each node after running this code?
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[];
  level: number;
  constructor(value: number) {
    this.value = value;
    this.children = [];
    this.level = 0;
  }
}

function assignLevels(root: TreeNode, currentLevel: number = 0): void {
  root.level = currentLevel;
  for (const child of root.children) {
    assignLevels(child, currentLevel + 1);
  }
}

const root = new TreeNode(1);
const child1 = new TreeNode(2);
const child2 = new TreeNode(3);
const grandchild = new TreeNode(4);
root.children.push(child1, child2);
child1.children.push(grandchild);

assignLevels(root);

console.log(root.level, child1.level, child2.level, grandchild.level);
A0 0 0 0
B0 1 1 2
C1 2 2 3
D1 1 1 1
Attempts:
2 left
šŸ’” Hint
Think about how levels increase as you go down from the root.
🧠 Conceptual
intermediate
1:30remaining
Identify Leaf Nodes in a Tree
Which nodes are leaf nodes in the following tree structure? Root (1) ā”œā”€ Child A (2) │ └─ Grandchild A1 (4) └─ Child B (3) ā”œā”€ Grandchild B1 (5) └─ Grandchild B2 (6)
ANodes 4, 5, 6
BNodes 1, 2, 3
CNodes 2, 3, 4
DNodes 1, 5, 6
Attempts:
2 left
šŸ’” Hint
Leaf nodes have no children.
ā“ Predict Output
advanced
2:00remaining
Calculate Height of a Tree
What is the output of the height calculated by this code for the given tree?
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[];
  constructor(value: number) {
    this.value = value;
    this.children = [];
  }
}

function height(node: TreeNode | null): number {
  if (!node) return -1;
  if (node.children.length === 0) return 0;
  let maxChildHeight = -1;
  for (const child of node.children) {
    const childHeight = height(child);
    if (childHeight > maxChildHeight) maxChildHeight = childHeight;
  }
  return maxChildHeight + 1;
}

const root = new TreeNode(1);
const c1 = new TreeNode(2);
const c2 = new TreeNode(3);
const gc1 = new TreeNode(4);
const gc2 = new TreeNode(5);
root.children.push(c1, c2);
c1.children.push(gc1);
c2.children.push(gc2);

console.log(height(root));
A2
B0
C1
D3
Attempts:
2 left
šŸ’” Hint
Height is the longest path from root to a leaf in edges.
šŸ”§ Debug
advanced
2:00remaining
Find the Depth of a Node
What is the depth of node with value 5 after running this code?
DSA Typescript
class TreeNode {
  value: number;
  children: TreeNode[];
  constructor(value: number) {
    this.value = value;
    this.children = [];
  }
}

function findDepth(root: TreeNode, target: number, currentDepth: number = 0): number {
  if (root.value === target) return currentDepth;
  for (const child of root.children) {
    const d = findDepth(child, target, currentDepth + 1);
    if (d !== -1) return d;
  }
  return -1;
}

const root = new TreeNode(1);
const c1 = new TreeNode(2);
const c2 = new TreeNode(3);
const gc1 = new TreeNode(4);
const gc2 = new TreeNode(5);
root.children.push(c1, c2);
c1.children.push(gc1);
c2.children.push(gc2);

console.log(findDepth(root, 5));
A-1
B3
C2
D1
Attempts:
2 left
šŸ’” Hint
Depth counts edges from root to the node.
🧠 Conceptual
expert
1:30remaining
Level vs Depth vs Height
Which statement correctly describes the difference between level, depth, and height of a node in a tree?
ADepth is the number of children; level is the distance from root to leaf; height is the distance from leaf to root.
BHeight and depth are the same; level is the number of children a node has.
CLevel is the number of children; depth is the number of leaves; height is the number of nodes in the tree.
DLevel and depth are the same; height is the number of edges on the longest path from the node to a leaf.
Attempts:
2 left
šŸ’” Hint
Think about how far a node is from the root and from the leaves.

Practice

(1/5)
1. In a tree data structure, which node is called the root?
easy
A. A node with no children
B. The topmost node with no parent
C. Any node at the bottom level
D. A node with exactly two children

Solution

  1. Step 1: Understand the root node definition

    The root node is the starting point of a tree and has no parent node above it.
  2. Step 2: Compare options with definition

    The topmost node with no parent matches the definition exactly. Other options describe leaf nodes or specific child counts.
  3. Final Answer:

    The topmost node with no parent -> Option B
  4. Quick Check:

    Root = topmost node with no parent [OK]
Hint: Root is always the top node without a parent [OK]
Common Mistakes:
  • Confusing root with leaf node
  • Thinking root must have children
  • Assuming root is at the bottom
2. Which of the following is the correct TypeScript syntax to define a simple tree node interface with a value and children array?
easy
A. interface TreeNode { value: number; children: TreeNode[]; }
B. interface TreeNode { value: number; children: TreeNode; }
C. interface TreeNode { value: number; children: number[]; }
D. interface TreeNode { value: number; child: TreeNode[]; }

Solution

  1. Step 1: Check correct property names and types

    The children property should be an array of TreeNode objects, so type is TreeNode[].
  2. Step 2: Validate each option

    interface TreeNode { value: number; children: TreeNode[]; } correctly uses 'children: TreeNode[]'. interface TreeNode { value: number; children: TreeNode; } uses single TreeNode, not array. interface TreeNode { value: number; children: number[]; } uses number array incorrectly. interface TreeNode { value: number; child: TreeNode[]; } uses wrong property name 'child'.
  3. Final Answer:

    interface TreeNode { value: number; children: TreeNode[]; } -> Option A
  4. Quick Check:

    Children is array of TreeNode objects [OK]
Hint: Children property must be an array of TreeNode [OK]
Common Mistakes:
  • Using singular 'child' instead of 'children'
  • Setting children as single object, not array
  • Using wrong type like number[] for children
3. Given the following tree structure in TypeScript, what is the height of the tree?
const tree = {
  value: 1,
  children: [
    { value: 2, children: [] },
    { value: 3, children: [
      { value: 4, children: [] }
    ] }
  ]
};
medium
A. 1
B. 4
C. 3
D. 2

Solution

  1. Step 1: Understand height definition

    Height is the longest path from root to any leaf node, counting edges.
  2. Step 2: Trace longest path in given tree

    Root (1) to node (3) to node (4) is longest path with 2 edges, so height = 2. Counting nodes gives 3 levels, but height counts edges, so height = 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Height = longest edge count from root to leaf = 2 [OK]
Hint: Height counts edges, not nodes, from root to deepest leaf [OK]
Common Mistakes:
  • Counting nodes instead of edges for height
  • Confusing height with depth
  • Ignoring deeper branches
4. Consider this TypeScript code snippet for calculating the depth of a node in a tree. Identify the bug:
function getDepth(node: TreeNode, root: TreeNode, depth = 0): number {
  if (node === root) return depth;
  return getDepth(node, root.children[0], depth + 1);
}
medium
A. It always checks only the first child, missing other branches
B. It never increments depth
C. It compares node and root incorrectly
D. It should return depth + 1 instead of depth

Solution

  1. Step 1: Analyze recursive call logic

    The function always calls getDepth on root.children[0], ignoring other children, so it misses nodes not in first child's subtree.
  2. Step 2: Understand impact on depth calculation

    This causes incorrect depth for nodes not in first child's branch, as they are never reached.
  3. Final Answer:

    It always checks only the first child, missing other branches -> Option A
  4. Quick Check:

    Recursion must explore all children, not just first [OK]
Hint: Check if recursion covers all children, not just one [OK]
Common Mistakes:
  • Assuming single child covers whole tree
  • Not handling multiple branches in recursion
  • Confusing depth increment logic
5. You have a tree where each node has a level property indicating its distance from the root (root level = 0). If a node has level 3, what is its depth and height relative to the root?
hard
A. Depth = 0, Height = 3
B. Depth = 3, Height = 3
C. Depth = 3, Height depends on subtree size
D. Depth and Height both equal to 0

Solution

  1. Step 1: Understand level and depth relation

    Level and depth both measure distance from root, so depth = level = 3.
  2. Step 2: Understand height meaning

    Height is longest path from node down to leaf, which depends on subtree size, so height is unknown from level alone.
  3. Final Answer:

    Depth = 3, Height depends on subtree size -> Option C
  4. Quick Check:

    Depth = level; height varies by subtree [OK]
Hint: Level = depth; height depends on node's subtree [OK]
Common Mistakes:
  • Confusing height with depth or level
  • Assuming height equals level always
  • Ignoring subtree size for height
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard